Solved

SQL server - SELECT Query to concat the few columns

Posted on 2011-09-13
2
391 Views
Last Modified: 2012-08-14
SELECT col1, col2,col3,col4,col5 FROM tablename

All columns data type are integer

I wrote this following statement, but any of the column has NULL value, I get empty string which is incorrect result.

SELECT (convert(nvarchar(10),col1) + ',' +convert(nvarchar(10),col2)+ ',' +convert(nvarchar(10),col3)+ ',' +convert(nvarchar(10),col4)+ ',' +convert(nvarchar(10),col5)) as colstring FROM tablename


How to concat all columns and make it one string? If any of the column value is NULL, do not include that?
Thanks,
0
Comment
Question by:andrishelp
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 125 total points
ID: 36531754

SELECT CAST(ISNULL(col1,'') as nvarchar(10)) + CAST(ISNULL(col2,'') as nvarchar(10)) + CAST(ISNULL(col3,'') as nvarchar(10)) + CAST(ISNULL(col4,'') as nvarchar(10))
FROM tablename
0
 
LVL 6

Assisted Solution

by:hyphenpipe
hyphenpipe earned 125 total points
ID: 36531755
SELECT (convert(nvarchar(10),isnull(col, '')1) + ',' +convert(nvarchar(10),isnull(col2,''))+ ',' +convert(nvarchar(10),isnull(col3, ''))+ ',' +convert(nvarchar(10),isnull(col4, ''))+ ',' +convert(nvarchar(10),isnull(col5, ''))) as colstring FROM tablename
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dataset not reading table data 12 46
Why does this keep coming up NULL? 2 43
Record open by another user 6 50
Caste datetime 2 51
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now