How can you use NULLIF in the where clause?

时间:2019-03-19 14:45:14

标签: sql sql-server nullif

I am trying to do something like this:

select
     col_1, col_2, etc
from
     table
where
     col_1 = nullif('', '')

Am I doing this incorrectly? I am not getting any results back.

Edit:

My expected results are to get every record back where col_1 is NULL.

I know I can use where col_1 is null, but I am using SSIS and a variable. Sometimes the col_1 is actually NULL and sometimes it is not.

Sample data:

 collaboration     first_name     last_name          city     
          NULL            Bob         Smith       Chicago
Data Migration           John         Smith        Austin
          NULL           Pika           Chu       Houston
    Production            ash       ketchum         tokyo

Sometimes I may want to return the records where collaboration is NULL, sometimes I want to return the records where it says Production.

I'd like to use the same query, if possible, with little modification.

Edit Part 2:

I tried to experiment with this.

select
     col_1, col_2, etc
from
     table
where
     case
         when col_1  = '' then NULL
         else col_1
         end

But I am getting the error message:

An expression of non-boolean type specified in a context where a condition is expected, near ORDER.

Query speed it not something I am concerned with.

6 个答案:

答案 0 :(得分:3)

This is the query you need

select
     col_1, col_2, etc
from
     table
where
     col_1 is null

is null checks if a column is null, nullif(@expr1,@expr2) could be rewritten as:

case when @expr1 = @expr2 return null else return @expr1 end

EDIT: you can relax filters adding OR condition into the 'where' clause (TIP: remember AND is evaluated before OR)

select
     col_1, col_2, etc
from
     table
where
     (col_1 is null OR col1 like 'production')

if you want to decide runtime wich one you neeed you could write a procedure:

create proc my_proc @var AS varchar(100) = 'NULL§159§' -- this defaults to null, if you put a parameter it queries with parameter passed
as
select
         col_1, col_2, etc
    from
         table
    where
         WHERE coalesce(col_1,'NULL§159§') = @var 
-- added §159§ symbol to the null to make sure the queried string is impossible in the database, 
-- obviously into the database the value 'NULL159' hase become a sort of 'reserved word', but hopefully is odd enough not to appear in data
GO

and call it by exec my_proc('production')

答案 1 :(得分:1)

Try this, it can handle the column with null values or empty space

SELECT
     col_1, col_2, etc
FROM
     Table
WHERE
     ISNULL(NULLIF(col_1 ,''),'1') = '1'

答案 2 :(得分:0)

You can do something like

select
     col_1, col_2, etc
from
     table
where
     col_1 IS NULL OR col_1 = ''

答案 3 :(得分:0)

select
     col_1, col_2, etc
from
     table
where
     collaboration IS NULL OR collaboration ='Production'

答案 4 :(得分:0)

Crystal ball time from me. This is my guess on what the OP wants:

DECLARE @Prod varchar(15);
--SET @Prod = 'Production';

SELECT {Columns}
FROM YourTable
WHERE Col1 = @Prod
   OR (Col1 IS NULL AND @Prod IS NULL);

答案 5 :(得分:0)

尝试一下。

DECLARE @SearchValue VARCHAR(50)
SELECT col_1, col_2, etc
FROM YourTable
WHERE ISNULL(col_1,'') = ISNULL(@SearchValue,'')