Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

Concatenation with NULL values

From Wiki

Jump to: navigation, search

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

  1. DECLARE @var1 VARCHAR(50)
  2.     DECLARE @var2 VARCHAR(50)
  3.      
  4.     SELECT @var1 ='Test'
  5.      
  6.     SELECT @var1 + @var2

That returned NULL


You can use ISNULL or COALESCE

  1. DECLARE @var1 VARCHAR(50)
  2.     DECLARE @var2 VARCHAR(50)
  3.      
  4.     SELECT @var1 ='Test'
  5.     SELECT @var1 + ISNULL(@var2,'')
  6.      
  7.     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.

  1. DECLARE @var1 VARCHAR(50)
  2.     DECLARE @var2 VARCHAR(50)
  3.      
  4.     SELECT @var1 ='Test'
  5.     SET CONCAT_NULL_YIELDS_NULL OFF
  6.      
  7.     SELECT @var1 + @var2
  8.      
  9.     SET CONCAT_NULL_YIELDS_NULL ON


Contributed by: --SQLDenis 16:34, 30 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section NULLS

118 Rating: 2.0/5 (10 votes cast)