Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A CONCAT Function For The Rest Of Us


A CONCAT Function For The Rest Of Us

Author
Message
mike.mcquillan
mike.mcquillan
Say Hey Kid
Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)

Group: General Forum Members
Points: 685 Visits: 440
Comments posted to this topic are about the item A CONCAT Function For The Rest Of Us
steve.ledridge
steve.ledridge
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 331
Why would I use this function rather than simply joining the strings with "+" as it does inside the function. Especially when there are more that two strings being joined, where it looks like it would be more expensive to call a function multiple times.
mike.mcquillan
mike.mcquillan
Say Hey Kid
Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)

Group: General Forum Members
Points: 685 Visits: 440
Hi Steve

Nobody is making you use the function. But to give one quick example where it could be useful. Say you have two string variables, and one of them is NULL. If you simply use + to add these together, you'll receive NULL - i.e. the valid string value will be lost. Using this CONCAT function, the string will be returned.

Example:


DECLARE @S1 VARCHAR(10)
DECLARE @S2 VARCHAR(10) = 'Hello'

SELECT dbo.CONCAT(@S1, @S2, 1) -- Returns 'Hello'

Regards,
Mike.
steve.ledridge
steve.ledridge
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 331
I didn't mean to imply that I was being forced. I was just trying to see if I was missing an aspect that would make it more useful in some places. But what it seems like is that there is no benefit over the tried and true method that most of us have been using for years with either coalesce or isnull.

select COALESCE(@String1,'') + ISNULL(@String2,'')
steve.ledridge
steve.ledridge
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 331
OK, I have found a benefit of the CONCAT function in SQL 2012. it is automatically converting data types to strings and outputting them rather than giving you data type mismatches, and forcing you to convert all the inputs. I can also see how people will consider this lazy and at least in the case of DateTime fields, you would generally convert to a specific format.

but instead of coding :
SELECT 'Current Time ' + CAST(GETDATE() AS VARCHAR(20))
you could use
SELECT CONCAT('Current Time ', GETDATE())
daryl.scott
daryl.scott
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 45
The SQL Server 2012 CONCAT function accepts two or more string values. The user-defined CONCAT function accepts only two values. It's the "two or more" part that would be valuable.
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8728 Visits: 885
Thanks for the script.
mike.mcquillan
mike.mcquillan
Say Hey Kid
Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)Say Hey Kid (685 reputation)

Group: General Forum Members
Points: 685 Visits: 440
Glad it was useful!
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