Marcel James
Marcel James

Reputation: 874

Concat two SQL's Results in one Column

I Have the following table "Custumer":

Customer_ID     Customer_Name

01               John
02               Mary
03               Marco

I would like to return the following result from a select:

Query_Result

01
02
03
John
Mary
Marco

Is it possible? Merge both columns in one?

Upvotes: 1

Views: 92

Answers (3)

user2989408
user2989408

Reputation: 3137

Try using UNION ALL, the following should work for you.

SELECT CONVERT(varchar(30), Customer_ID) FROM Customer
UNION ALL 
SELECT Customer_Name FROM Customer

Upvotes: 1

Jacek Glen
Jacek Glen

Reputation: 1546

Basically you have two options to achieve this:

  1. Select data from each column and use UNION ALL to merge results. Be careful to make sure that the results of each select produce the same data type.

  2. Unpivot table as on example below:

    SELECT AnyColumn
    FROM
    (SELECT Customer_ID, Customer_Name
        FROM Customer) p
    UNPIVOT
    (
        AnyColumn FOR Customer IN
        (Customer_ID, Customer_Name)
    ) AS unpvt;
    

Personally, I would question necessity of doing such operation. If you find yourself to be forced to do it, I'd recommend you re-think your data model.

Upvotes: 1

Alex K.
Alex K.

Reputation: 175766

One way (assumes Customer_ID is a character type)

SELECT 
   Customer_ID
FROM Customer

UNION ALL

SELECT 
   Customer_Name
FROM Customer

Upvotes: 3

Related Questions