Unix Technical Forum

Should SQL concatenation return a null value if one field is null?

This is a discussion on Should SQL concatenation return a null value if one field is null? within the SQL Server forums, part of the Microsoft SQL Server category; --> In an SQL statement which concatenates several fields I get a null value returned if any one of the ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 03:37 AM
John Morgan
 
Posts: n/a
Default Should SQL concatenation return a null value if one field is null?

In an SQL statement which concatenates several fields I get a null
value returned if any one of the fields are null.

Is this to be expected?

For example :

SELECT tblMember.memberAddress + ' ' + tblMember.memberTown + ' ' +
tblMember.memberCounty + ' ' + tblMember.memberPostCode + '<br> ' +
tblMember.memberCountry + '<br> ' + tblMember.memberInstitution AS
concatAddress FROM tblMember WHERE memberSurname='Cardy'

returns a null value if eg tblMember.memberInstitution is null.

Am I doing something wrong, if so I would be grateful for your help.
Otherwise it would be useful to know if there is some kind of work
around which can be used within the SQL statement (which is being
used in a stored procedure),

Best wishes, John Morgan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 03:37 AM
Simon Hayes
 
Posts: n/a
Default Re: Should SQL concatenation return a null value if one field is null?


"John Morgan" <jfm@XXwoodlander.co.uk> wrote in message
news:i7sq801rns7bfjli57ado8atitkdll96um@4ax.com...
> In an SQL statement which concatenates several fields I get a null
> value returned if any one of the fields are null.
>
> Is this to be expected?
>
> For example :
>
> SELECT tblMember.memberAddress + ' ' + tblMember.memberTown + ' ' +
> tblMember.memberCounty + ' ' + tblMember.memberPostCode + '<br> ' +
> tblMember.memberCountry + '<br> ' + tblMember.memberInstitution AS
> concatAddress FROM tblMember WHERE memberSurname='Cardy'
>
> returns a null value if eg tblMember.memberInstitution is null.
>
> Am I doing something wrong, if so I would be grateful for your help.
> Otherwise it would be useful to know if there is some kind of work
> around which can be used within the SQL statement (which is being
> used in a stored procedure),
>
> Best wishes, John Morgan


See SET CONCAT_NULL_YIELDS_NULL in Books Online. If this is ON, then you
will get a NULL, and it is recommended to have it on for the reasons
mentioned in the documentation. If there may be NULL values in your data
then you can use ISNULL() or COALESCE() to prevent them propagating:

select isnull(col1, '') + isnull(col2, '')...
from dbo.MyTable

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 03:37 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Should SQL concatenation return a null value if one field is null?

John Morgan (jfm@XXwoodlander.co.uk) writes:
> In an SQL statement which concatenates several fields I get a null
> value returned if any one of the fields are null.
>
> Is this to be expected?


Yes. This is a fundamental issue in SQL: if you have an expression that
includes a NULL value, and the value of the expression is dependent of
that value, then then value of the expression is NULL. NULL is an unknown
value, and no matter you concatenate to it, the value is still unknown.

Thus:

DECLARE @a int, @null int, @b char(2)

SELECT @a = 98, @null = NULL, @b = 'YH'

SELECT @a + @null -- NULL
SELECT power(@a, @null) -- NULL
SELECT @b + @null -- NULL

IF @a = @null PRINT 'is true' ELSE PRINT 'is not true' -- is not true
IF @a != @null PRINT 'is true' ELSE PRINT 'is not true' -- is not true
IF @a = 98 AND @null = 23 PRINT 'is true' ELSE 'is not true' -- is not true
IF @a = 98 OR @null = 23 PRINT 'is true' ELSE 'is not true' -- is true

The last is true, because here the condition @a = 98 alone determines the
value of the OR expression.

There are commands to change this, but I stronly recommend you to stay
away from them. They should only be used by legacy applications.

--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 03:37 AM
John Morgan
 
Posts: n/a
Default Re: Should SQL concatenation return a null value if one field is null?

Thanks Simon and Erland for your help, its much appreciated,
Best wishes, John Morgan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com