Concatenate existing fields SQL server

  • I have a table with static data. For reasons way too long to go into, I need to add a column, and the value of that column will need to be a concatenation of two other existing fields in that same table. It's got something like 225000 rows, so I cannot do it manually. I'm googling and not seeing much that will help on this. I imagine it's a simple T-SQL statement, but am drawing a blank.

    Using SQL Server 2005.

    To be clear: I am not talking about selecting from the table and doing the concatenating on the fly. I want to add a column.

  • Hi,

    I think you have two options here,

    1. If the table is truly static (never going to change) then you could just run an update on the table to set column C to be equal to column A and B

    2. If the data is likely to change in the future then you could use a computed column.

    Hope this helps.

  • Here is an example of the computed column (incase you need it)

    USE tempdb

    GO

    CREATE TABLE TableA

    (

    ColumnA VARCHAR(5),

    ColumnB VARCHAR(5),

    ColumnC AS ((ColumnA+'-')+ColumnB)

    )

    INSERT INTO TableA

    (ColumnA,ColumnB)

    VALUES

    ('Hello','World')

    SELECT ColumnA,

    ColumnB,

    ColumnC

    FROM TableA

    DROP TABLE TableA

  • I think Option 1 is best. I added a column and will now do an update. First, I have to cast the one column (which is numeric) as a varchar.

    thanks

  • Option 1 would certainly be simpler.

    Happy to help.

  • It seems to have worked. Thanks.

Viewing 6 posts - 1 through 5 (of 5 total)

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