sivaprakash.s
sivaprakash.s

Reputation: 35

Combine two Column into One column with out union

I have an Employee Table having two column city1,city2.

I want to select combine city1,city2 into single column with out using union.

Any other option for this?

Upvotes: 0

Views: 46

Answers (3)

Thom A
Thom A

Reputation: 95561

If we assume that the OP does mean that want the results in the same way as a UNION (which I doubt), then you could use a VALUES expression:

SELECT V.City
FROM YourTable YT
     CROSS APPLY (VALUES(YT.City1),(YT.City2)) V(City);
--WHERE V.City IS NOT NULL; --?

Upvotes: 1

Qirel
Qirel

Reputation: 26450

Join the two strings together. You can use CONCAT() to achieve this. In this example, we add a whitespace between the two, this can be changed to your needs.

SELECT CONCAT(city1, ' ', city2) as city 
FROM myTable

Alternatively you can use + to join strings together, although I recommend CONCAT() (in case any of them are null).

SELECT city1 + ' ' + city2 as city
FROM myTable

Upvotes: 2

Arulkumar
Arulkumar

Reputation: 13237

Using CONCAT(), you can combine the two column values into one column as

SELECT CONCAT(city1, city2) AS City
FROM Employee 

In case if you want to add space in between column values, use CONCAT(city1, ' ', city2)

Upvotes: 0

Related Questions