Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

01 December 2011

SQL Server ISNULL function

Definition:
In SQL Server ISNULL() function is used to replace null value with other value. Basically ISNULL allows a display of the particular value as substitution for NULL.
Syntax:
ISNULL(Expression , replace_Value )

If Expression is null then replace_Value will be returned, otherwise Expression will be returned
Example:
SELECT ISNULL(1, 10) – Output is 1
SELECT ISNULL(NULL, 10) – Output is 10
Script:
CREATE TABLE Student
(
sub VARCHAR(10),
marks INT
)
--Insert stattements
INSERT INTO Student VALUES('Maths',67)
INSERT INTO Student VALUES('English',NULL)

SELECT sub,isnull(marks,0) AS Marks FROM student
Output:
Table data:
 





After applying ISNULL:






For English the student marks are null so it returned 0. For Maths subject student marks is not null so orginal value is returned.
insert into Student values('English','s') --Error :Conversion failed when converting the varchar value 's' to data type int
Because marks in int and replacement value is varchar.


In order to check column values as null, does not use ISNULL instead use IS NULL.

1 Comments:

_.)___) said...
This comment has been removed by a blog administrator.

Post a Comment