Solved

concat null yields null

Posted on 2001-09-10
11
1,114 Views
Last Modified: 2007-11-27
The statement
'select field1 + ' ' + field2 + ' ' + field3 from ...'
gives me null output even that "concat null yields null" is set to false.
Is there any other parameter that need to be set/unset to retrieve string instead of null when one of substrings in null?
Cheers
0
Comment
Question by:PeterZG
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6471393
in SQL Server, i use this, avoiding to base myself on the settings:

select COALESCE(field1, '') + ' ' + COALESCE( field2,'') + ' ' + COALESCE(field3,'') from ...

Cheers
0
 
LVL 1

Author Comment

by:PeterZG
ID: 6471448
I was thinking about it, but the problem with this solution is that I'll need to change several views and stored procedures. There was a change to the existing system and one of the fields is now nullable. I don't want to go through a manual ammending excercise...
0
 
LVL 6

Expert Comment

by:jchopde
ID: 6471502
In that case, you could create a view with "isnull" in a SELECT. Existing logic will work then. You may need to rename the "real" table and create the view with the original  table's name to maintain the "no code change" goal, which might open up some other can of worms... HTH.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:PeterZG
ID: 6471523
Just wonder why "concat null yields null" doesn't work?
I'm using M$SQL7 SP3
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6471635
BOL:

concat null yields null
When true, if one of the operands in a concatenation operation is NULL, the result of the operation is NULL. For example, concatenating the character string ?This is? and NULL results in the value NULL, rather than the value ?This is?.
When false, concatenating a null value with a character string yields the character string as the result; the null value is treated as an empty character string. By default, concat null yields null is false.

Session-level settings (set using the SET statement) override the default database setting for concat null yields null. By default, ODBC and OLE DB clients issue a SET statement setting concat null yields null to true for the session when connecting to SQL Server. For more information, see SET CONCAT_NULL_YIELDS_NULL.

The status of this option can be determined by examining the IsNullConcat property of the DATABASEPROPERTY function.

SET CONCAT_NULL_YIELDS_NULL {ON | OFF}

Remarks
When SET CONCAT_NULL_YIELDS_NULL  is ON, concatenating a null value with a string yields a NULL result. For example, SELECT ?abc? + NULL yields NULL. When SET CONCAT_NULL_YIELDS_NULL is OFF, concatenating a null value with a string yields the string itself (the null value is treated as an empty string). For example, SELECT ?abc? + NULL yields abc.

If not specified, the setting of the concat null yields null database option applies.


--------------------------------------------------------------------------------

Note SET CONCAT_NULL_YIELDS_NULL is the same setting as the concat null yields null setting of sp_dboption.


--------------------------------------------------------------------------------

The setting of SET CONCAT_NULL_YIELDS_NULL  is set at execute or run time and not at parse time.


0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6471637
ooops... copy/paste changes ' into ?  , sorry for that
0
 
LVL 1

Author Comment

by:PeterZG
ID: 6471697
select databaseproperty('MyDB', 'IsNullConcat') gives 0, so I shouldn't get null value, but I'm getting it...
What am I missing here?
0
 
LVL 6

Expert Comment

by:jchopde
ID: 6471698
Folks, this does not work as advertised for SQL 7.0 :-(
You can make it work by setting your database compatibility level to 65. So if you run
sp_dbcmptlevel '<<dbname>>', 65
sp_dboption '<<dbname>>','concat null yields null','true'

and do your query, it will work ! Docs say the EXACT opposite ! Don't know if you have the luxury of setting the compatibility level to 65. HTH.
0
 
LVL 6

Expert Comment

by:jchopde
ID: 6471782
Well, I jumped the gun looks like. It DOES work as advertised but as angelIII and BOL point out, ODBC and SQL Query Analyzer will turn this ON by default so you need to explicitly turn the behavior OFF if you are using either of these connection mechanisms. Pete, I guess its code change or compatibility level ... take your pick :-)
0
 
LVL 6

Expert Comment

by:curtis591
ID: 6471983
Sounds a bit tricky with the settings to me.  We just use the isnull function in sql.
ex:
isnull(field1,'') + isnull(field2,'') + isnull(field3,'')
0
 
LVL 1

Author Comment

by:PeterZG
ID: 7163695
Guys,
sorry for leaving that question opened so long.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 32
MS SQL + Insert Into Table - If Doesnt Exist 9 35
Whats wrong in this query - Select * from tableA,tableA 11 31
sql server service accounts 4 26
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question