Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating fields which may have a null 1

Status
Not open for further replies.

Sashanan

Programmer
Jan 19, 2001
235
NL
In an SQL Server 7.0 database, I'm trying to concatenate a couple of fields into one string. The problem is that one of them is often Null, and doing so causes the entire string to default to Null; which makes sense but is not what I want. Rather, I want it to be treated like an empty value in that case.

So, when I do:

SELECT col1 + col2 + col3 AS fullstring FROM MyTable WHERE [some condition]

And the record this picks up has:

col1: A
col2: [null]
col3: C

I want the result to be:

fullstring: AC

whereas currently it becomes Null.

How can I accomplish this?


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Code:
SELECT COALESCE(col1, '') + COALESCE(col2, '') + COALESCE(col3, '') AS fullstring
FROM mytable

You can also use the SQL-specific function ISNULL. Or even issue the following statement before your original query:

Code:
SET CONCAT_NULL_YIELDS_NULL OFF

--James
 
Plenty of options I see. Thanks!


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top