ISNULL
- ISNULL function is used to replace the NULL value with specified value.
- It contains only two arguments.
- Same data type not compulsory.
Example
-1:
SELECT ISNULL(NULL,'Raki') AS [ISNULL]
Output: Raki
Example -2:
DECLARE @name VARCHAR(10)
DECLARE @marks INT =500
SELECT ISNULL(@name,@marks) AS [ISNULL]
Output: 500
Example-3:
SELECT ISNULL(NULL,NULL,'Raki') AS [ISNULL]
Output:
Msg 174, Level 15, State 1,
The isnull function requires 2 argument(s).
COALESCE
- Coalesce function is returns first non null value among arguments.
- It contains multiple arguments.
- Same data type compulsory for arguments or precedence data type order should follow.
Example-1:
SELECT COALESCE(NULL,NULL,'Raki') as [COALESCE]
Output: Raki
Example-2:
DECLARE @name VARCHAR(5)='Raki'
DECLARE @marks INT =500
SELECT COALESCE(@name,@marks) as [COALESCE]
Output:
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the
varchar value 'Raki' to data type int.
Comments
Post a Comment