954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL Server Question...

Hi,
I have one question related to SQL Server. But there is not forum on this. so here is my Question.

While accessing multiple columns value in a single alias column we right query as follows.

Select FNAME +' '+ MNAME +' '+ LNAME as NAME from Cust_Master


Am I right ?

But the question is when one of values from FNAME, MNAME, LNAME if comes NULL then my NAME filed will be NULL.

How to avoid this?? Any one have solution for this.

Note : I am using this Query for Creating VIEW.

Thanks. Have a Lovely Time. :pretty:

sbv
Junior Poster
178 posts since Jan 2008
Reputation Points: 15
Solved Threads: 8
 

Select CASE WHEN FNAME IS NULL THEN '' ELSE FNAME END +' '+ CASE WHEN MNAME IS NULL THEN '' ELSE MNAME END +' '+ CASE WHEN LNAME IS NULL THEN '' ELSE LNAME END as NAME from Cust_Master

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Scratch that! Use a predefined function called COALESCE. It's a life saver. Format:
COALESCE(columnname, 'valueifnull')

Select COALESCE(FNAME, '') +' '+ COALESCE(MNAME, '') +' '+ COALESCE(LNAME, '') as NAME from Cust_Master
SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

This way if middle nam eis null or something, it will show:

FNAME LNAME

or MNAME LNAME

or FNAME LNAME, etc.


Oh and there is a forum for MS SQL:

http://www.daniweb.com/forums/forum127.html

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

Scratch that! Use a predefined function called COALESCE. It's a life saver. Format: COALESCE(columnname, 'valueifnull')

Select COALESCE(FNAME, '') +' '+ COALESCE(MNAME, '') +' '+ COALESCE(LNAME, '') as NAME from Cust_Master

Your t-sql is better than mine shesaid.. Is that what you want to hear from me :D

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

no no no, just spreading the knowledge lol.

You taught me though, cause I never thought it was possible to add case statements with inline sql like that.

So your t-sql is better use to me :)

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

By the way if you replace the "COALESCE" with "ISNULL", it gives exactly the same result

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

Thanks a lot to both of you. Solutions you provided solved my problem.
You guise are brilliant. When I can be as you. :(

Have a nice time. Thanks :)

sbv
Junior Poster
178 posts since Jan 2008
Reputation Points: 15
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You