WHERE IS NULL, IS NOT NULL or NO WHERE clause depending on SQL Server parameter value

前端 未结 7 2284
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 22:57

I have a stored procedure in SQL Server 2000 that performs a search based on parameter values. For one of the parameters passed in, I need a different WHERE cla

相关标签:
7条回答
  • 2020-12-02 23:18

    I've had success with this solution. It's almost like Patrick's, with a little twist. You can use these expressions separately or in sequence. If the parameter is blank, it will be ignored and all values for the column that your searching will be displayed, including NULLS.

    SELECT * FROM MyTable
    WHERE 
        --check to see if @param1 exists, if @param1 is blank, return all
        --records excluding filters below
    (Col1 LIKE '%' + @param1 + '%' OR @param1 = '')
    AND
        --where you want to search multiple columns using the same parameter
        --enclose the first 'OR' expression in braces and enclose the entire 
        --expression 
    ((Col2 LIKE '%' + @searchString + '%' OR Col3 LIKE '%' + @searchString + '%') OR @searchString = '')
    AND
        --if your search requires a date you could do the following
    (Cast(DateCol AS DATE) BETWEEN CAST(@dateParam AS Date) AND CAST(GETDATE() AS DATE) OR @dateParam = '')
    
    0 讨论(0)
  • 2020-12-02 23:23

    Here is how you can solve this using a single WHERE clause:

    WHERE (@myParm = value1 AND MyColumn IS NULL)
    OR  (@myParm = value2 AND MyColumn IS NOT NULL)
    OR  (@myParm = value3)
    

    A naïve usage of the CASE statement does not work, by this I mean the following:

    SELECT Field1, Field2 FROM MyTable
    WHERE CASE @myParam
        WHEN value1 THEN MyColumn IS NULL
        WHEN value2 THEN MyColumn IS NOT NULL
        WHEN value3 THEN TRUE
    END
    

    It is possible to solve this using a case statement, see onedaywhen's answer

    0 讨论(0)
  • 2020-12-02 23:28

    You could just do something like this:

    SELECT *
    FROM foo
    WHERE (@param = 0 AND MyColumn IS NULL)
    OR (@param = 1 AND MyColumn IS NOT NULL)
    OR (@param = 2)
    

    Something like that.

    0 讨论(0)
  • 2020-12-02 23:30

    This is how it can be done using CASE:

    DECLARE @myParam INT;
    SET @myParam = 1;
    
    SELECT * 
      FROM MyTable
     WHERE 'T' = CASE @myParam
                 WHEN 1 THEN 
                    CASE WHEN MyColumn IS NULL THEN 'T' END
                 WHEN 2 THEN
                    CASE WHEN MyColumn IS NOT NULL THEN 'T' END
                 WHEN 3 THEN 'T' END;
    
    0 讨论(0)
  • 2020-12-02 23:30
    WHERE MyColumn = COALESCE(@value,MyColumn) 
    
    • If @value is NULL, it will compare MyColumn to itself, ignoring @value = no where clause.

    • IF @value has a value (NOT NULL) it will compare MyColumn to @value.

    Reference: COALESCE (Transact-SQL).

    0 讨论(0)
  • 2020-12-02 23:35

    An other way of CASE:

    SELECT *  
    FROM MyTable
    WHERE 1 = CASE WHEN @myParm = value1 AND MyColumn IS NULL     THEN 1 
                   WHEN @myParm = value2 AND MyColumn IS NOT NULL THEN 1 
                   WHEN @myParm = value3                          THEN 1 
              END
    
    0 讨论(0)
提交回复
热议问题