How to combine two columns into ONE

  • I have a scenario wherein I have to combine two columns into one . Its SQL Server 2005

    Below is the example :

    CREATE TABLE Table_Source

    (

    PK_ID INT IDENTITY(1, 1),

    Col_A VARCHAR(2000),

    Col_B TEXT

    )

    CREATE TABLE Table_Destination

    (

    PK_ID INT IDENTITY(1, 1),

    Col_C VARCHAR(MAX)

    )

    Sample data for both the tables:

    Table_Soruce :

    PK_id Col_A Col_B

    1 Abc Null

    2 Pqr Null

    3 Null XYZ

    4 Null EFG

    I want to have data in Table_Destination in the below manner :

    Table_Destination

    PK_id Col_C

    1 Abc

    2 Pqr

    3 XYZ

    4 EFG

    Can anyone throw a script to achieve this scenario. Table_Source will have million of rows to fetch from.

    Thanks for your help

  • insert into Table_Destination (Col_c)

    select ISNULL(Col_A,cast(Col_B as varchar(max))

    from Table_Source

    -- or

    insert into Table_Destination (Col_c)

    select COALESCE(Col_A,cast(Col_B as varchar(max))

    from Table_Source

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 2 posts - 1 through 1 (of 1 total)

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