Introduction:
Here
I will explain how to write a query to return default value if column contains
null value in SQL
Server.
Description:
In
previous articles I explained how to delete null or empty values from datatable in SQL
Server and many articles relating to SQL
Server.
Now I will explain how to write a query to return default if column contains
null value in SQL
Server.
For that first design one table (UserInfo)
in database and enter data like as shown below
UserId
|
Name
|
City
|
Education
|
1
|
Suresh
|
Guntur
|
B.Tech
|
2
|
Nagaraju
|
NULL
|
MCA
|
3
|
Sai
|
NULL
|
MBA
|
4
|
Madhav
|
NULL
|
MBBS
|
Query to return default values if
column contains null value
If
we want to return default value if column contains null value in SQL Server
that would be like as shown below
SELECT UserId,Name,ISNULL(City,'Tenali'),Education FROM UserInfo
|
In
above query I written condition if City column contains null value then it will
return value as 'Tenali'
Output
UserId
|
Name
|
City
|
Education
|
1
|
Suresh
|
Guntur
|
B.Tech
|
2
|
Nagaraju
|
Tenali
|
MCA
|
3
|
Sai
|
Tenali
|
MBA
|
4
|
Madhav
|
Tenali
|
MBBS
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
4 comments :
Really thanks for all ur posts
thanks alot
we can use COALESCE to replace null value.
Thanks alot dear very usefull
Note: Only a member of this blog may post a comment.