ISNULL checks that value is null then return given expression.

Let's understand the practical example, if we have NULL> then we have to show any default value then we use the ISNULL.


Below is the syntax of ISNULL.


Syntax for ISNULL

SELECT ISNULL ( < value or columns to check for null > , < new replace value > )

1) Example

Below example where we create one string variable and not assign any value to it (so default value is null), if value is null then we need default value 'WebNetGuru' as output.

Example

DECLARE @CourseName VARCHAR (100)
SELECT ISNULL (@CourseName, 'WebNetGuru' ) AS 'DefalutName'

Output

is null example in sql server


Note : If we observe the above example we can find out that ISNULL check if first parameter having NULL value then it will show the value of second parameter or else it will first parameter value.

2) Example

Below example where we create one string variable and assign some value to it.


Example

DECLARE @city VARCHAR (100) = 'Delhi'
SELECT ISNULL (@city,'Mumbai' ) AS 'StudentCity'

Output

isnull example in sql server


3) Example

Lets find out the practical example of ISNULL.
We have student table and it contain student name, roll number and city, but unfortunately some student not having city. Consider the below result set.

is null in sql server

Now we required output that if student city is not available then show the 'Mumbai' as default city using ISNILL.


Example

DECLARE @city VARCHAR (100) = 'Delhi'
SELECT ISNULL (@city,'Mumbai' ) AS 'StudentCity'

Output

isnull in sql server