how to concatenate data from records into a string

  • Hi

    I have a code table that has about 10 codes, and I want to concatenate them all together into one string, with a space in between each one.  Is there an easy way to do this?   Here's what I've tried and it's not working:

     

    declare @ws_list varchar (500)

    update Tbl_Codes

     set  @ws_list =  @ws_list + ' ' + db_code

    from Tbl_Codes

     

    Thanks

    Rick

     

  • TRY THIS

    DECLARE

    @a VARCHAR(4000)

    SELECT

    @a = ISNULL(@a,'') + ' '+ ISNULL(db_code,'')

    FROM

    tbl_codes

    SELECT @a

     

  •  

    It works!   Thanks

  • If you prefer not to have a space as first character in the concatenated string, use this

    Also prevents for concatenating too long strings.

     

    DECLARE @a VARCHAR(8000)

    SELECT @A = left(ISNULL(@A + ' ', '') + db_code, 8000)

    FROM tbl_codes

    SELECT @a


    N 56°04'39.16"
    E 12°55'05.25"

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply