Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Format function in SQL Server Denali CTP3Finding the Winning Streak »
    comments

    SQL Server Denali CTP3 brings a couple of new functions, one of these is the CONCAT function. The CONCAT function returns a string that is the result of concatenating two or more string values.

    The syntax of the CONCAT function looks like this

    CONCAT ( string_value1, string_value2 [, string_valueN ] )

    You can concatenate between 2 and 254 values, if you use for example only one value, you will get an error

    1. select CONCAT (1)

    Msg 189, Level 15, State 1, Line 1
    The concat function requires 2 to 254 arguments.

    Here is some additional information

    CONCAT takes a variable number of string arguments and concatenates them into a single string. It requires a minimum of two input values; otherwise, an error is raised. All arguments are implicitly converted to string types and then concatenated. Null values are implicitly converted to an empty string. If all the arguments are null, then an empty string of type varchar(1) is returned. The implicit conversion to strings follows the existing rules for data type conversions

    Let's run some code and do some comparison with a regular string concatenation by using the @val + @val2

    1. declare @i char(1)  ='1'
    2. declare @i3 char(1)  ='3'
    3.  
    4.  
    5. select CONCAT (@i,@i3)
    6. select @i+  @i3

    ------
    13
    13

    As you can see both of these return the value 13

    What happens if one of the data type is an integer?

    1. declare @i char(1)  ='1'
    2. declare @i3 int  ='3'
    3.  
    4.  
    5. select CONCAT(@i,@i3)
    6. select @i+  @i3

    -----------------
    13
    4

    As you can see CONCAT concatenates the values while the other method does arithmetic and adds the values since one of them is an integer

    Here is another example that does the same

    1. declare @i char(1)  ='1'
    2. declare @i2 int  =2
    3. declare @i3 char(1)  ='3'
    4.  
    5.  
    6. select CONCAT(@i,@i2,@i3)
    7.  
    8. select @i+ @i2+ @i3

    -----------------
    123
    6

    What happens if one of the values is NULL?

    1. declare @i char(1)  ='1'
    2. declare @i2 int  =null
    3. declare @i3 char(1)  ='3'
    4.  
    5.  
    6. select CONCAT(@i,@i2,@i3)
    7.  
    8. select @i+ @i2+ @i3

    --------------
    13
    null

    As you can see the CONCAT functions makes the NULL an empty string while the other method does not.

    In order to get the same output, the old method is a lot more code

    1. declare @i char(1)  ='1'
    2. declare @i2 int  =null
    3. declare @i3 char(1)  ='3'
    4.  
    5.  
    6. select CONCAT(@i,@i2,@i3)
    7.  
    8.  
    9. select isnull(@i,'')+ isnull(convert(varchar(10),@i2),'')+ isnull(@i3,'')

    -----
    13
    13

    So what is your opinion, are you happy that the CONCAT function has been added to SQL Server?

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2215 views
    InstapaperVote on HN

    8 comments

    Comment from: vince [Member]
    vince /me is happy !
    07/15/11 @ 06:52
    Comment from: Garry Bargsley [Visitor]
    Garry Bargsley Does anyone else notice the BOL for this function has an example that is incorrect.


    SELECT CONCAT ( 'Happy', 'Christmas', 12, '/', '25' ) AS Result;

    Result
    -------------------------
    Happy Christmas 12/25

    (1 row(s) affected)


    Results show with spaces, but I would not think from looking at the example that spaces would be between the words. I run it in my system and no spaces.
    07/15/11 @ 07:58
    Comment from: David Forck (thirster42) [Member]
    awesome. now you can jsut write concat(column1,column2) instead of cast(column1 as varchar(50)) + column2
    07/15/11 @ 08:03
    Comment from: SQLDenis [Member] Email
    SQLDenis That is correct, no spaces, BOL is incorrect

    The 2nd example on BOL is correct
    07/15/11 @ 08:05
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Nice, but what about GROUP_CONCAT?
    07/15/11 @ 09:17
    Comment from: Paul Hunter [Visitor]
    Paul Hunter it would be better if you had a couple of options...
    maybe something like:
    select concat(column_name from table_name)
    OR
    select concat(column_name) from table_name

    [or maybe it already does?]

    ...or a delimited concat:
    select concatd(',', string_value1, string_value2 [, string_valueN ])
    07/15/11 @ 09:52
    Comment from: Jack Corbett [Visitor] · http://www.wiseman-wiseguy.blogspot.com
    Jack Corbett I love that this function is being added. Of course I probably won't be working with this version for 3 years or more.
    07/18/11 @ 20:00
    Comment from: Rob Garrison [Visitor] · http://persistencevision.blogspot.com/
    Rob Garrison Great new feature. The implicit conversion is a huge win.

    Thanks for the examples.

    Rob
    08/01/11 @ 18:19

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)