Use of coalesce in SQL

Source: Internet
Author: User
Coalesce
Returns the first non-empty expression in the parameter. Syntax
Coalesce (expression [,... n]) parameter
Expression any type of expression. N indicates that placeholders of multiple expressions can be specified. All expressions must be of the same type, or can be implicitly converted to the same type. Return type
Returns the same value as expression. Note
If all the independent variables are null, coalesce returns NULL. Coalesce (expression1,... n) is equivalent to this case function: Case
When (expression1 is not null) Then expression1
...
When (expressionn is not null) Then expressionn
Else null

The following is an example: 01 /********************************** ******************************
02 ** the following example shows three columns of information about an employee's annual salary and income. # wages table:
03 ** hourly_wage, salary, and commission. However, each employee can only accept one payment method.
04 ** to determine the total salary paid to all employees, use the coalesce function to accept
05 ** non-null values found in hourly_wage, salary, and commission.
06 *************************************** *************************/
07 set nocount on
08 go
09 If exists (select table_name from information_schema.tables
10 where table_name = '# wages ')
11 drop table # wages
12 go
13 create table # wages
14 (
15 emp_id tinyint identity,
16 hourly_wage decimal null,
17 salary decimal null,
18 Commission decimal null,
19 num_sales tinyint null
20)
21 go
22 insert # wages values (10.00, null)
23 insert # wages values (20.00, null)
24 insert # wages values (30.00, null)
25 insert # wages values (40.00, null)
26 insert # wages values (null, 10000.00, null, null)
27 insert # wages values (null, 20000.00, null, null)
28 insert # wages values (null, 30000.00, null, null)
29 insert # wages values (null, 40000.00, null, null)
30 Insert # wages values (null, null, 15000, 3)
31 insert # wages values (null, null, 25000, 2)
32 insert # wages values (null, null, 20000, 6)
33 insert # wages values (null, null, 14000, 4)
34 go
35 set nocount off
36 go
37 select * from # wages
38 go
39 select cast (coalesce (hourly_wage * 40*52, salary, Commission * num_sales) as money) as 'total salary'
40 from # wages
41 go

Running result
Emp_id hourly_wage Salary Commission num_sales
---------------------------------------------------------------------------
1 10 null
2 20 null
3 30 null
4 40 null
5 null 10000 null
6 null 20000 null
7 null 30000 null
8 null 40000 null
9 null 15000 3
10null null 25000 2
11 null 20000 6
12 null 14000 4

(The number of affected rows is 12)

Total salary
---------------------
20800.0000
41600.0000
62400.0000
83200.0000
10000.0000
20000.0000
30000.0000
40000.0000
45000.0000
50000.0000
120000.0000
56000.0000

(The number of affected rows is 12)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.