By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,412 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,412 IT Pros & Developers. It's quick & easy.

Combine two column with sql statement

Expert 100+
P: 210
I was able to combine three columns with following sql statement but the problem that i am having is if one of the column has a null value the concatenation does not work and the sql statement does not return any thing. I am not sure why this is happening. Is there a way to accomplish this even though the fields contain null value.

Sql statement:

Expand|Select|Wrap|Line Numbers
  1. Select CAST(Customer.Contactid as nvarchar(10)) +'-'+ Customer.Firstname +','+ Customer.LastName as Name from Customer where customerid = 1547854
Data types:

Contactid -- int
FirstName -- nvarchar
LastName -- nvarchar

Note: The above sql statement works perfect if non of the fields have null value.

Thank you in advance
Sep 9 '08 #1
Share this Question
Share on Google+
2 Replies


ck9663
Expert 2.5K+
P: 2,878
Yes.

Use ISNULL() function.

Try this:

Expand|Select|Wrap|Line Numbers
  1. Select isnull(CAST(Customer.Contactid as nvarchar(10)),'') +'-'+ isnull(Customer.Firstname,'') +','+ isnull(Customer.LastName,'') as Name from Customer where customerid = 1547854
-- CK
Sep 9 '08 #2

Expert 100+
P: 210
It worked

Thank you
Sep 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.