How to concatenate two strings in SQL Server 2005

后端 未结 6 1305
夕颜
夕颜 2020-12-06 09:16

I want to concatenate the words \"dummy\'s\" and \"dock\".

How can I concatenate them in SQL Server 2005? Does it support double quotes?

相关标签:
6条回答
  • 2020-12-06 09:34

    I got a easy solution which will select from database table and let you do easily.

    SELECT b.FirstName + b.LastName FROM tbl_Users b WHERE b.Id='11'
    

    You can easily add a space there if you try

    SELECT b.FirstName +' '+ b.LastName FROM Users b WHERE b.Id='23'
    

    Here you can combine as much as your table have.

    0 讨论(0)
  • 2020-12-06 09:36

    To concatenate two strings in 2008 or prior:

    SELECT ISNULL(FirstName, '') + ' ' + ISNULL(SurName, '')
    

    good to use ISNULL because "String + NULL" will give you a NULL only

    One more thing: Make sure you are concatenating strings otherwise use a CAST operator:

    SELECT 2 + 3 
    

    Will give 5

    SELECT '2' + '3'
    

    Will give 23

    0 讨论(0)
  • 2020-12-06 09:37

    Try this:

    DECLARE @COMBINED_STRINGS AS VARCHAR(50); -- Allocate just enough length for the two strings.
    
    SET @COMBINED_STRINGS = 'rupesh''s' + 'malviya';
    SELECT @COMBINED_STRINGS; -- Print your combined strings.
    

    Or you can put your strings into variables. Such that:

    DECLARE @COMBINED_STRINGS AS VARCHAR(50),
            @STRING1 AS VARCHAR(20),
            @STRING2 AS VARCHAR(20);
    
    SET @STRING1 = 'rupesh''s';
    SET @STRING2 = 'malviya';
    SET @COMBINED_STRINGS = @STRING1 + @STRING2;
    
    SELECT @COMBINED_STRINGS; 
    

    Output:

    rupesh'smalviya

    Just add a space in your string as a separator.

    0 讨论(0)
  • 2020-12-06 09:37
    DECLARE @COMBINED_STRINGS AS VARCHAR(50),
            @STRING1 AS VARCHAR(20),
            @STRING2 AS VARCHAR(20);
    
    SET @STRING1 = 'rupesh''s';
    SET @STRING2 = 'malviya';
    SET @COMBINED_STRINGS = @STRING1 + @STRING2;
    
    SELECT @COMBINED_STRINGS;
    
    SELECT '2' + '3';
    

    I typed this in a sql file named TEST.sql and I run it. I got the following out put.

    +-------------------+
    | @COMBINED_STRINGS |
    +-------------------+
    |                 0 |
    +-------------------+
    1 row in set (0.00 sec)
    
    +-----------+
    | '2' + '3' |
    +-----------+
    |         5 |
    +-----------+
    1 row in set (0.00 sec)
    

    After looking into this issue a bit more I found the best and sure sort way for string concatenation in SQL is by using CONCAT method. So I made the following changes in the same file.

    #DECLARE @COMBINED_STRINGS AS VARCHAR(50),
     #       @STRING1 AS VARCHAR(20),
     #       @STRING2 AS VARCHAR(20);
    
    SET @STRING1 = 'rupesh''s';
    SET @STRING2 = 'malviya';
    #SET @COMBINED_STRINGS = @STRING1 + @STRING2;
    SET @COMBINED_STRINGS = (SELECT CONCAT(@STRING1, @STRING2));
    
    SELECT @COMBINED_STRINGS;
    
    #SELECT '2' + '3';
    SELECT CONCAT('2','3');
    

    and after executing the file this was the output.

    +-------------------+
    | @COMBINED_STRINGS |
    +-------------------+
    | rupesh'smalviya   |
    +-------------------+
    1 row in set (0.00 sec)
    
    +-----------------+
    | CONCAT('2','3') |
    +-----------------+
    | 23              |
    +-----------------+
    1 row in set (0.00 sec)
    

    SQL version I am using is: 14.14

    0 讨论(0)
  • 2020-12-06 09:48

    Try something like

    SELECT 'rupesh''s' + 'malviya'
    

    + (String Concatenation)

    0 讨论(0)
  • 2020-12-06 09:50

    so if you have a table with a row like:

    firstname lastname
    Bill      smith
    

    you can do something like

    select firstname + ' ' + lastname from thetable
    

    and you will get "Bill Smith"

    0 讨论(0)
提交回复
热议问题