SQL Server string concatenation

D

David

Hi all,

I have a small problem...

I need to do string concatenation in sql server...

i.e.

select field1 + field2 + field3 as JoinedField from table

This works fine IF all the fields have content, but if one of them is null,
then the whole JoinedField is null.

Any idea on how I can fix it? I want the joined value as though the null was
just an empty string.

Thanks.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
C

Cowboy \(Gregory A. Beamer\)

select
CASE WHEN field1 is null then ''
ELSE field1 END
+
CASE WHEN field2 is null then ''
ELSE field2 END
+
CASE WHEN field3 is null then ''
ELSE field3 END AS JoinedField

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 
P

Paul Shapiro

And a 3rd version (all 3 would work fine):

Select Coalesce(field1, '') + coalesce(field2, '') etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ASP.NET Membership 3
Problem with dataset crashing my app 7
Load Balancing 5
AJAX send a JS Alert 1
Problem with textchanged event 1
HtmlEncode bound data 1
Web references 3
Problem with authentication 2

Top