Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2004, 09:58 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default T-SQL String Concatenation

I'm trying to join together five columns under one column heading when querying a table;

SELECT ([DD].[DADDR1] + [DD].[DADDR2] + [DD].[DADDR3] + [DD].[DADDR4] + [DD].[DADDR5]) AS 'DELIVERYADDRESS'
FROM DELDETS DD
WHERE DD.DRECNO=274001

However, whenever this query is run I get a null value returned - yet the query when run to return the five columns (i.e. SELECT DD.DADDR1, DD.DADDR2, DD.DADDR3 ...) does return data. It would seem that concatenating more than two columns in T-SQL is not possible - does anyone know if this is indeed the case?

 
Old January 13th, 2004, 10:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your syntax is fine, but if any of the columns contain a null value then the whole concatenated value will be null.

Use COALESCE to change any nulls to empty strings
SELECT (COALESCE([DD].[DADDR1], '') + COALESCE([DD].[DADDR2], '') + etc
 
Old January 13th, 2004, 10:54 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Fantastic, working great now. Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance in String concatenation rushman XSLT 4 September 27th, 2007 11:02 AM
String concatenation query. AjayLuthria XSLT 2 March 14th, 2007 11:55 AM
Convert NULL to String for Concatenation? Ron Howerton SQL Language 5 September 23rd, 2006 05:54 AM
string concatenation nulogix PHP How-To 1 June 24th, 2004 06:17 AM
string concatenation matt_99 Access VBA 2 January 17th, 2004 09:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.