-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-CoalesceFunction.sql
More file actions
39 lines (30 loc) · 1014 Bytes
/
16-CoalesceFunction.sql
File metadata and controls
39 lines (30 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Coalesce() Function -> Returns the first NON NULL value
Select ID, COALESCE(FirstName, MIddleName, LastName) As Name FROM tblEmployee
*/
Create table tblName
(
ID int NOT NULL identity(1,1) Constraint PK_tblName_ID Primary key,
FirstName nvarchar(50) NULL,
MiddleName nvarchar(50) NULL,
lastName nvarchar(50) NULL
)
Insert into tblName(FirstName) values('Sam');
Insert into tblName(MiddleName, lastName) values('Todd', 'Tanzan');
Insert into tblName(lastName) values('Sara');
Insert into tblName(FirstName,MiddleName) values('Ben', 'parker');
Insert into tblName(FirstName,MiddleName,lastName) values('james', 'Nike', 'Nency');
/*
1 Sam NULL NULL
2 NULL Todd Tanzan
3 NULL NULL Sara
4 Ben parker NULL
5 james Nike Nency
*/
/*
Coalesce() Function -> Returns the first NON NULL value
Select ID, COALESCE(FirstName, MIddleName, LastName) As Name FROM tblEmployee
*/
Select * from tblName
Select ID, COALESCE(FirstName, MIddleName, LastName) As NAME
FROM tblName