SQL Server, how to merge two columns into one column?
Asked Answered
P

4

11

I have a SQL query

SELECT TABLE_SCHEMA + TABLE_NAME AS ColumnZ 
FROM information_schema.tables

I want the result should be table_Schema.table_name.

help me please!!

Positively answered 8/8, 2014 at 16:13 Comment(1)
Use SELECT TABLE_SCHEMA + '.' + TABLE_NAME ....Platitudinize
L
19

Try this:

SELECT TABLE_SCHEMA + '.' + TABLE_NAME AS ColumnZ 
FROM information_schema.tables
Larimer answered 8/8, 2014 at 16:15 Comment(1)
Thanks. Very simple '+' oprator compbine coumn in to single columnNevski
D
6

This code work for you try this....

SELECT Title,
FirstName,
lastName, 
ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName 
FROM Customer
Distress answered 20/12, 2014 at 13:27 Comment(0)
P
5
SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS ColumnZ FROM information_schema.tables
Palmer answered 8/8, 2014 at 16:16 Comment(4)
Watch out: CONCAT is a new feature in SQL Server 2012 and thus might not be available to the person askingPlatitudinize
@Platitudinize I did not know this, it will be useful if I ever have to work with old MSSQL versions, thanks.Palmer
Being a new feature doesn't make the answer worthy of a downvote, in my opinion.Cogswell
@GordonLinoff: just to be absolutely clear - it wasn't my downvote; I agree with you - it's a new feature, but it's correct - for that newer version of SQL Server. No downvote warranted ...Platitudinize
C
2

From SQL Server 2017 onwards, there is CONCAT_WS operator, to concatenate with separators.

SELECT CONCAT_WS('.', TABLE_SCHEMA ,TABLE_NAME) AS ColumnZ 
FROM information_schema.tables
Candescent answered 18/8, 2020 at 7:1 Comment(1)
This is great for handling nulls, where you don't want the extra separators, and can do the same for empty strings in a column if it is wrapped in NULLIF().Asclepiadean

© 2022 - 2024 — McMap. All rights reserved.