You need to return NULL only if the value of your data is a certain value. How do you do this?
There are three different ways.
NULLIF
DECLARE @1 char(1)
SELECT @1 ='D'
SELECT NULLIF(@1,'D')
REPLACE
This should not really be used, I just added it here to demonstrate that you can in fact use it.
DECLARE @1 char(1)
SELECT @1 ='D'
SELECT REPLACE(@1,'D',NULL)
CASE
With case you can test for a range of values. You can test for example for values between A and D. If you reverse the logic then you also don't need to provide the ELSE part since it defaults to NULL anyway.
DECLARE @1 char(1)
SELECT @1 ='D'
SELECT CASE @1 WHEN 'D' THEN NULL ELSE @1 END
--No else needed
SELECT CASE WHEN @1 <> 'D' THEN @1 END
And this is how you test for a range.
--Null
DECLARE @1 char(1)
SELECT @1 ='D'
SELECT CASE WHEN @1 BETWEEN 'A' AND 'D' THEN NULL ELSE @1 END
--E
DECLARE @1 char(1)
SELECT @1 ='E'
SELECT CASE WHEN @1 BETWEEN 'A' AND 'D' THEN NULL ELSE @1 END
A blog about SQL Server, Books, Movies and life in general
Showing posts with label NULLIF. Show all posts
Showing posts with label NULLIF. Show all posts
Tuesday, November 06, 2007
Monday, August 20, 2007
Do you know how NULLIF and non-deterministic functions work?
Run this first
CREATE TABLE #j (n varchar(15))
DECLARE @a int
SET @a = 1
WHILE @a <= 1000 BEGIN
INSERT #j
SELECT NULLIF(REPLICATE('1', RAND()*2) , ' ')
SET @a = @a + 1
END
Go
After that is done run this query
SELECT * FROM #j WHERE n = ' '
You will get back between 200 and 300 rows. What just happened? In our insert we use this NULLIF(REPLICATE('1', RAND()*2) , ' ')
What this does is the following: if REPLICATE('1', RAND()*2) equals ' ' then it will insert a NULL, so where do the blanks come from? Well let's find out.
Run this
SET SHOWPLAN_TEXT ON
SELECT NULLIF(REPLICATE('1', RAND()*2) , ' ')
and we see the following
--Compute Scalar(DEFINE:([Expr1000]=If (replicate('1', Convert(rand(NULL)*2))=' ') then NULL else replicate('1', Convert(rand(NULL)*2))))
This can also be written like this
SELECT CASE WHEN REPLICATE('1', RAND()*2) =' '
THEN NULL ELSE REPLICATE('1', RAND()*2) END
See what happens? First SQL evaluates if REPLICATE('1', RAND()*2) is ' ' if that is not ' ' then the same code gets executed again and thus could be a blank
If you use a variable this can never happen.
Here is an example, keep hitting F5 and you will see it will never be blank
DECLARE @val float
SET @val = RAND()
SELECT NULLIF(REPLICATE('1', CONVERT(int, @val*2)) , ' ')
CREATE TABLE #j (n varchar(15))
DECLARE @a int
SET @a = 1
WHILE @a <= 1000 BEGIN
INSERT #j
SELECT NULLIF(REPLICATE('1', RAND()*2) , ' ')
SET @a = @a + 1
END
Go
After that is done run this query
SELECT * FROM #j WHERE n = ' '
You will get back between 200 and 300 rows. What just happened? In our insert we use this NULLIF(REPLICATE('1', RAND()*2) , ' ')
What this does is the following: if REPLICATE('1', RAND()*2) equals ' ' then it will insert a NULL, so where do the blanks come from? Well let's find out.
Run this
SET SHOWPLAN_TEXT ON
SELECT NULLIF(REPLICATE('1', RAND()*2) , ' ')
and we see the following
--Compute Scalar(DEFINE:([Expr1000]=If (replicate('1', Convert(rand(NULL)*2))=' ') then NULL else replicate('1', Convert(rand(NULL)*2))))
This can also be written like this
SELECT CASE WHEN REPLICATE('1', RAND()*2) =' '
THEN NULL ELSE REPLICATE('1', RAND()*2) END
See what happens? First SQL evaluates if REPLICATE('1', RAND()*2) is ' ' if that is not ' ' then the same code gets executed again and thus could be a blank
If you use a variable this can never happen.
Here is an example, keep hitting F5 and you will see it will never be blank
DECLARE @val float
SET @val = RAND()
SELECT NULLIF(REPLICATE('1', CONVERT(int, @val*2)) , ' ')
Labels:
Functions,
NULLIF,
SQL Server 2000,
SQL Server 2005,
SQL Server 2008
Subscribe to:
Posts (Atom)