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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Concatenating fields

Status
Not open for further replies.

johnsun1976

Programmer
Jan 28, 2002
34
CA
Hi.. I have a problem this SQL statement

SELECT Desc1 + ' ' + Desc2 AS Description FROM Item WHERE ItemID = 1234

if Desc1 or Desc2 is NULL, Description is null.. Does anyone know how to fix this?
Thanks
John
 
Method 1:
SELECT
Isnull(Desc1,'') + ' ' +
Isnull(Desc2,'') AS Description
FROM Item WHERE ItemID = 1234

Method 2: Turn off ANSI default concatenation behavior
SET CONCAT_NULL_YIELDS_NULL OFF
SELECT
Desc1 + ' ' + Desc2 AS Description
FROM Item WHERE ItemID = 1234
SET CONCAT_NULL_YIELDS_NULL ON

Using either method, you may want to add the LTRIM and RTRIM functions to trim leading and trailing spaces. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
You could use the IsNull() on each field and return empty string if it is. I think this would prevent that anything concatenated with a null will be null.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top