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 2005 Forums
 Transact-SQL (2005)
 wierd Null value problem

Author  Topic 

mike13
Posting Yak Master

219 Posts

Posted - 2007-10-02 : 06:49:26
Hi all,

I ran into this weird problem sure there must be an easy solution.
I have this select statement, but sometimes there is a field that has Value NULL (in this case was Title2) and when that happens the statement does not return anything :-(
What can i do to resolve this

SELECT Title1 + ' ' + Title2 + ' ' + FirstName + ' ' + LastName + ' - Company: ' + CompanyName
FROM T_Customer_Main where id=1

thanks a lot,

Mike

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-02 : 06:55:33
Read about ISNULL and/or COALESCE functions in Books Online.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-02 : 07:33:14
"when that happens the statement does not return anything"

The result of NULL involved with anything is NULL. Very important concept to grasp!

Also NULL does not equal anything else, not even another NULL.

So

WHERE Col1 = Col2

is FALSE if BOTH Col1 and Col2 are NULL.

What you probably want is something like this:

SELECT COALESCE(Title1 + ' ', '')
+ COALESCE(Title2 + ' ', '')
+ ...

so that if there IS a value for Title1 it is appended, and a space is added, otherwise an empty string is appended.

Kristen
Go to Top of Page

mike13
Posting Yak Master

219 Posts

Posted - 2007-10-02 : 07:48:51
thanks a lot.
sounds logic, learning more T-SQL everyday
Go to Top of Page
   

- Advertisement -