Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
Sometimes when retrieving data from the Database we don't want to return NULL but instead of NULL we want to return some friendly message saying 'This record is not found'. In these situations CASE function is very helpful. Look at a simple example below:
 
SELECT P.Name,Ph.PhoneNumber, 
'CellNumber' = 
CASE WHEN Ph.CellNumber IS NULL THEN 
'No CellNumber Found' 
ELSE 
Ph.CellNumber
END

FROM tblPerson P, tblPhone Ph 
WHERE P.PersonID = Ph.PersonID
Posted on Friday, August 19, 2005 1:06 PM | Back to top


Comments on this post: Returning Friendly Message Instead of NULL Using T-SQL Query

# re: Returning Friendly Message Instead of NULL Using T-SQL Query
Requesting Gravatar...
Hi, Azam,
you can also test for nulls with IsNull() function, like:

select P.Name, isnull(Ph.CellNumber, 'No CellNumber Found')
from ...
Left by Andrej on Aug 20, 2005 2:19 AM

# re: Returning Friendly Message Instead of NULL Using T-SQL Query
Requesting Gravatar...
Thanks for pointing that out!
Left by Azamsharp on Aug 20, 2005 6:05 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net