Solved

concat null yields null

Posted on 2001-09-10
11
1,110 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:PeterZG
Comment Utility
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]
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 142

Expert Comment

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

Author Comment

by:PeterZG
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Guys,
sorry for leaving that question opened so long.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now