?
Solved

SQL server - SELECT Query to concat the few columns

Posted on 2011-09-13
2
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 500 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

762 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