Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 string concatenation

Author  Topic 

gamolchan
Starting Member

8 Posts

Posted - 2006-10-05 : 16:50:25
When I use the t-sql code:

SELECT (au_lname + ', ' + au_fname) AS Name

If au_fname is null, then Name is null even if au_lname is not null

I am confused, but that is not new! :-)

TIA for any help

gamolchan
Starting Member

8 Posts

Posted - 2006-10-05 : 16:54:32
Well I lied, when I use the example posted, I get results, but when I change the code to reflect the field names in my live DB, if the Fname is null, then Name is null? So I am still confused :-)
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-10-05 : 17:02:55
That is what it is supposed to do.

When you concatenate strings, and any part is NULL, then the result will be NULL.



CODO ERGO SUM
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-05 : 17:48:50
Use the ISNULL function to return a value in place of the NULL, so for example

ISNULL(au_fname, '')


will return the value of au_lname if it is not null, or '' if it is null. So now you can do this

SELECT (au_lname + ', ' + ISNULL(au_fname, '')) AS Name


The problem is now, although the result will not be null, it will have a comma after the last name when there is no first name, which you don't really want, so then you should use a CASE like this

SELECT CASE WHEN au_fname IS NULL THEN au_lname ELSE au_lname + ', ' + au_fname END AS Name


That will not use the expression at all when au_fname is null so that you don't get the unnecessary comma.
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-05 : 17:56:12
quote:
I am confused

Just to explain further - while it may seem strange that SQL Server would make the result null when you concatenate null with a value, it actually makes very good sense. You might expect that it would return the au_lname and the comma when the au_fname is null, but that would require SQL Server to assume that null means a blank string.

SQL Server doesn't make assumptions about the meaning of null - ever. Is null really a blank string or zero or 1/1/1900 or whatever? No, it is unknown, therefore you have to really see your example as what is au_lname concatenated with a comma concatenated with unknown, and clearly the result must be unknown.

The example I use is this - if we had an employee table with a salary column and we were giving everyone a 10% raise. If your current salary was NULL, would you want SQL Server to give the result for (NULL plus 10%) as NULL or 0 (assuming null in a numeric column seems reasonable if we are assuming null in a character column is a blank string)?!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-05 : 21:11:23
Note that NULL is not equal to ''

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

gamolchan
Starting Member

8 Posts

Posted - 2006-10-06 : 09:15:50
Thanks for the info.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-10-06 : 09:39:13
COALESCE, use COALESCE

Do you have books online?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

gamolchan
Starting Member

8 Posts

Posted - 2006-10-06 : 10:44:30
Thanks for all the help, and I got it to work. I think when I tested the tsql on the pubs DB, I created blanck Lname records and not null Lname records.
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-10-06 : 11:23:17
My favorite syntax for covering all bases:

SELECT coalesce(au_lname + ', ' + au_fname, au_lname, au_fname) AS Name


"Once you establish possibility, all that remains are desire and determination."
Go to Top of Page
   

- Advertisement -