Get data in 2 columns

  • Hello,

    I am currently working on extracting data from a text file and loading into sql server.

    This is where I am stuck:

    I have a table with 1 column and the data in the column is in this format:

    TableName1

    A

    1

    10

    TableName2

    D

    2

    20

    I want to convert this to 2 columns in this format:

    ColumnA ColumnB

    TableName1 A

    TableName1 1

    TableName1 10

    TableName2 D

    TableName2 2

    TableName2 20

    Can anyone please help me to write a sql for this?

    Thanks,

    San

  • You could do something like this:

    SELECT 'TableName1' AS ColumnA,

    SomeColumn AS ColumnB

    FROM TableName1

    UNION ALL

    SELECT 'TableName2' AS ColumnA,

    SomeColumn AS ColumnB

    FROM TableName2

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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