none
How to get string for concatenation RRS feed

  • Question

  • I'm trying to write a function that, in part, puts two strings together.  Here's what I have so far:

    DECLARE @X NVARCHAR(100)
    SET @X = 'Some Text' + SELECT Entry FROM MyTable WHERE ID = 48;

    I know the SELECT statement is wrong but I don't know how to fix it.

    ADDITION:

    While my function is now working, it is only returning one character, not the whole string.  I must be missing something else.

    • Edited by RogerSSB Saturday, December 14, 2019 10:11 PM New issue
    Saturday, December 14, 2019 1:06 AM

Answers

  • try this...

    DECLARE @X NVARCHAR(100) 
    
    Declare @Table Table (id int,Entry varchar(20))
    
    Insert into @table
    Values (48,'this is from table')
    
    --method#1
    --if you need to assign this to a variable
    SELECT @X = 'Some Text ' +Entry FROM @table WHERE ID = 48;
    
    select @X as X_Variable
    
    --method#2
    --return from the table directly
    SELECT  'Some Text ' +Entry as [message] 
    FROM @table WHERE ID = 48;
    



    Hope it Helps!!


    • Edited by Stan210 Saturday, December 14, 2019 1:14 AM
    • Marked as answer by RogerSSB Saturday, December 14, 2019 9:51 PM
    Saturday, December 14, 2019 1:09 AM

All replies

  • try this...

    DECLARE @X NVARCHAR(100) 
    
    Declare @Table Table (id int,Entry varchar(20))
    
    Insert into @table
    Values (48,'this is from table')
    
    --method#1
    --if you need to assign this to a variable
    SELECT @X = 'Some Text ' +Entry FROM @table WHERE ID = 48;
    
    select @X as X_Variable
    
    --method#2
    --return from the table directly
    SELECT  'Some Text ' +Entry as [message] 
    FROM @table WHERE ID = 48;
    



    Hope it Helps!!


    • Edited by Stan210 Saturday, December 14, 2019 1:14 AM
    • Marked as answer by RogerSSB Saturday, December 14, 2019 9:51 PM
    Saturday, December 14, 2019 1:09 AM
  • Hi,

    DECLARE @X NVARCHAR(100)
    SET @X = 'Some Text' 

    select @x + Entry FROM MyTable WHERE ID = 48;

    Saturday, December 14, 2019 3:29 AM
  • DECLARE @X NVARCHAR(100)
     SELECT @X = Concat('Some Text' , Entry) FROM MyTable WHERE ID = 48;

    Saturday, December 14, 2019 5:34 AM
    Moderator
  • The strcat function is used to concatenate one string (source) at the end of another string (destination). It does the following:

    1. Takes the destination string.
    2. Finds the NULL character.
    3. Copies the source string starting at the location of the NULL character of destination string.
    4. Appends a NULL character to the destination string when all characters of the source string are copied to the destination string.
    Saturday, December 14, 2019 6:23 AM
  • If Entry is a text column and the row is unique, then this should work too:

       SET @X = 'Some Text' + ( SELECT Entry FROM MyTable WHERE ID = 48 )

    Saturday, December 14, 2019 8:26 AM
  • Now that I see the answer, the question is a bit embarrassing.  I should have tried that.  *Sigh*
    Saturday, December 14, 2019 9:52 PM
  • Have you tried ...?

    DECLARE @X NVARCHAR(100)
    SET @X = 'Some Text'

    • SELECT @x + Entry
    • FROM MyTable
    • WHERE ID = 48;

    :)

    Saturday, December 14, 2019 10:38 PM