Combining Columns into One

  • How do I take several columns with different data on each and create a new column to combine all the data into one column.

    So I have data in each. I have column AAA and column BBB and column CCC,

    and I want to create a single column (called original) with all the data in AAA, BBB and CCC.

    How can I do this or is it possible in SQL?

    Thanks in advance.

  • Depends on how exactly you want to combine the data, but would something like this work?

    ;with t

    (

    AAA,

    BBB,

    CCC

    )

    as

    (

    select 'Tacos', 'Burritos', 'Airplanes' union all

    select 'Up', 'Down', 'All Around'

    )

    select

    Original = AAA + ', ' + BBB + + ',' + CCC

    from t

    Executive Junior Cowboy Developer, Esq.[/url]

  • This doesn't seem to be working.

    What I have is 3 columns and each have numeric numbers. I need to sum them up into one new column called amount. How would I create a new column with the summed up amount?

    Thanks.

  • The same principle applies for numeric columns. Again, just setting up some fake numeric data, you can do this:

    --This is just setting up some fake source data

    ;with t (AAA, BBB, CCC) as

    (

    select abs(checksum(newid())) % 100, abs(checksum(newid())) % 100, abs(checksum(newid())) % 100 union all

    select abs(checksum(newid())) % 100, abs(checksum(newid())) % 100, abs(checksum(newid())) % 100

    )

    select

    AAA,

    BBB,

    CCC,

    --This is the actual summing operation

    SumColumns = AAA + BBB+ CCC

    from t

    If you have this data in a table and you're looking for how to physically add a new column to the table which sums them up, you could use a computed column with essentially the same function. Assuming you had a table called MyValues, the code to add such a computed column would look like this:

    alter table MyValues

    add column SumColumns as AAA + BBB + CCC

    Executive Junior Cowboy Developer, Esq.[/url]

  • Here's how I got this to work...

    Select isnull(AAA,0) + isnull(BBB,0) + isnull(CCC,0) as Original

    from MY Table

    Thanks for your help.

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

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