SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server 2012 Concat


SQL Server 2012 Concat

Author
Message
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1558 Visits: 3317
wolfkillj (7/9/2013)
Carlo Romagnano (7/9/2013)
L' Eomot Inversé (7/9/2013)
Nice straightforward question. Good to see a question about another new feature in SQL 2012.

However, introducing a weird ODBC function into T-SQL seems to me a bad idea. It's treatment of NULL is a mechanism to allow people to continue to misuse null once the ability to switch ANSI nulls off is removed, and continue to write obscure buggy code instead of cleaning things up.

I guess the ability to write (for example) "concat(5,6,7,8)" and get 5678 might be considered useful, because it saves a bit of typing - not in the case of literals since "'5'+'6'+'7'+'8'" is 15 characters and so is the expression with concat - but when the arguments are column names "concat(c1,c2,c3,c4)" is quite a lot shorter than "cast(c1 as varchar)+cast(c2 as varchar)+cast(c3 as varchar)+cast(c4 as varchar)" but I don't believe the benefits of being able to do that (of similar things with float) outweigh the scope for misuse of this form of null handling, so on balance I feel concat is something I would rather live without.

CONCAT makes life easy!


While I can certainly see the advantages of replacing NULL arguments with empty strings, as does the CONCAT function, I also see Tom's point. Improper handling of NULLs already causes enough trouble, and it just seems like a function that doesn't require a developer to think consistently about the issue may encourage bad habits. Those bad habits could be especially troublesome when MS finally deprecates the ANSI_NULLS OFF and CONCAT_NULL_YIELDS_NULL OFF settings. MS is reintroducing behavior in the CONCAT function that it is eliminating in all other contexts by deprecating the CONCAT_NULL_YIELDS_NULL OFF setting.

While this function is mistreating NULLs in the pure sense, I am going to be grateful for being to replace some long and complicated to read expressions that include CASTs, ISNULLs and occasionally NULLIFs.
I suppose it comes down to if the NULL is being used as an unknown or as a no value.

We could look at the function slightly differently and rather than saying it treats NULLs as empty strings, say that it ignores NULLs while concatenating:-) similar to the aggregate functions
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27077 Visits: 13268
Easy one, thanks for the question.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Bangla
Bangla
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1689 Visits: 180
Thanks .....
PHYData DBA
PHYData DBA
SSC Eights!
SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)SSC Eights! (893 reputation)

Group: General Forum Members
Points: 893 Visits: 537
Nice question. On of the first editions of the .NET data libraries had NULL value operator and could not read or assign NULL to a value.
IMHO - NULL values are useless and concat helps us reduce the amount of ISNULL code needed when having to parse together names when the Middle name may be NULL value 45% of the time, a non breaking space 10%, a '' or empty string.

Thanks M$ for adding something that acts more like & and less like the add function.
pchirags
pchirags
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2339 Visits: 615
Nice and interesting question..
Thanks..
FrugalDeveloper
FrugalDeveloper
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 96
I did not know the difference until I type this SQL statement on SQL Management Studio Smile
Uwe Ricken
Uwe Ricken
SSC Eights!
SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)SSC Eights! (879 reputation)

Group: General Forum Members
Points: 879 Visits: 878
The answer isn't correct because "it depends"!
I answered it correct but only because of the standard settings for the session parameter [CONCAT_NULL_YIELDS_NULL].


SET CONCAT_NULL_YIELDS_NULL OFF;
GO

SELECT session_id, concat_null_yields_null FROM sys.dm_exec_sessions WHERE session_id = @@spid;

-- now it is the default behaviour and the expected answer of QotD
select concat(null,'testString') as a
, null+'testString' as b

SET CONCAT_NULL_YIELDS_NULL ON;
GO

SELECT session_id, concat_null_yields_null FROM sys.dm_exec_sessions WHERE session_id = @@spid;

-- now both concatenations will return the same result!
select concat(null,'testString') as a
, null+'testString' as b


As others said - good question because of the amount of failed answers. But this - little - issue is worth to be mentioned Smile

Microsoft Certified Master: SQL Server 2008
MVP - Data Platform (2013 - ...)
my blog: http://www.sqlmaster.de (german only!)
udayroy15
udayroy15
Old Hand
Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)

Group: General Forum Members
Points: 330 Visits: 100
good question :-)
sqlnaive
sqlnaive
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4269 Visits: 2774
Good one. :-)
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31814 Visits: 18550
Nice N EZ



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search