SQL Server Performance

Combine data from two columns into one column

Discussion in 'SQL Server 2005 General Developer Questions' started by Fais, Oct 19, 2009.

  1. Fais New Member

    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
  2. FrankKalis Moderator

    If there is always only one column of the two populated, you can use
    SET IDENTITY_INSERT Table_Destination ON;
    INSERT INTO Table_Destination (PK_ID, Col_C)
    SELECT PK_ID, ISNULL(Col_A, Col_B)
    FROM Table_Source;
    SET IDENTITY_INSERT Table_Destination OFF;

Share This Page