Link to home
Start Free TrialLog in
Avatar of PeterZG
PeterZG

asked on

concat null yields null

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterZG
PeterZG

ASKER

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...
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.
Avatar of PeterZG

ASKER

Just wonder why "concat null yields null" doesn't work?
I'm using M$SQL7 SP3
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.


ooops... copy/paste changes ' into ?  , sorry for that
Avatar of PeterZG

ASKER

select databaseproperty('MyDB', 'IsNullConcat') gives 0, so I shouldn't get null value, but I'm getting it...
What am I missing here?
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.
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 :-)
Sounds a bit tricky with the settings to me.  We just use the isnull function in sql.
ex:
isnull(field1,'') + isnull(field2,'') + isnull(field3,'')
Avatar of PeterZG

ASKER

Guys,
sorry for leaving that question opened so long.