Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Concatenation with NULL values
From Wiki
How do you concatenate with NULL values? If you concatenate a two values and one of them is NULL then the result will be NULL
Run this to see what I mean
- DECLARE @var1 VARCHAR(50)
- DECLARE @var2 VARCHAR(50)
- SELECT @var1 ='Test'
- SELECT @var1 + @var2
That returned NULL
You can use ISNULL or COALESCE
- DECLARE @var1 VARCHAR(50)
- DECLARE @var2 VARCHAR(50)
- SELECT @var1 ='Test'
- SELECT @var1 + ISNULL(@var2,'')
- SELECT @var1 + COALESCE(@var2,'')
You can also the CONCAT_NULL_YIELDS_NULL setting but I would not mess with it, I only show it because it is possible to use it.
- DECLARE @var1 VARCHAR(50)
- DECLARE @var2 VARCHAR(50)
- SELECT @var1 ='Test'
- SET CONCAT_NULL_YIELDS_NULL OFF
- SELECT @var1 + @var2
- SET CONCAT_NULL_YIELDS_NULL ON
Contributed by: --SQLDenis 16:34, 30 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section NULLS



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.