Solved

SQL server - SELECT Query to concat the few columns

Posted on 2011-09-13
2
388 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

18 Experts available now in Live!

Get 1:1 Help Now