Using Case Statement in SQL with parameter/variable to check for Null values

假如想象 提交于 2019-12-23 10:20:02

问题


I am trying to write a SQL Select statement to return records based on a user input through a front end. I want to write the Select statement like this:

SELECT somefields
FROM sometable
WHERE CASE variable
         WHEN 'blank' THEN field IS NULL
         ELSE field = field
      END

Basically I either want to filter a column to find NULL values or ignore the filter and return all values depending on the value of the variable. I know that the results of the CASE statement is not executable but how can I do this?


回答1:


When variable is 'blank', the following query will give you rows where field is NULL. When variable is anything else, it will give you all rows:

SELECT somefields
FROM sometable
WHERE
    (variable = 'blank' AND field IS NULL)
    OR (variable <> 'blank')



回答2:


You can use NULLIF() (link is for SQL Server, but NULLIF() should be standard):

SELECT somefields
  FROM sometable
 WHERE field = NULLIF(variable, 'blank')



回答3:


The following snippet should behave as follows:

  • when @variable is null, return all rows
  • when @variable = 'blank', return all rows where field is null or field = 'blank'
  • otherwise, return rows where @variable equals field

Code snippet:

WHERE 1 = CASE
      WHEN @variable is null then 1
      WHEN @variable = 'blank' and field is null then 1
      WHEN @variable = field then 1
      END



回答4:


SELECT somefields
FROM sometable
WHERE ((variable IS NULL OR variable = 0) OR (variable = field))

WHERE Criteria is apply when variable have value
For Example:

DECLARE @CityName VARCHAR(50)
SET @CityName = NULL

SELECT CityName
FROM City
WHERE ((@CityName IS NULL ) OR (@CityName = CityName ))

When City is null then tables return all rows




回答5:


I think I get what you're after. Something like this maybe?

SELECT field1,
    field2,
    CASE variable
        WHEN 'blank' THEN NULL
        ELSE field3
    END as field3
FROM sometable



回答6:


Think I understand what you mean....for example....

    SELECT 
          House, Postcode 
    from 
          SomeTable 
    where
          (House=isnull(@House,House) or (House is null and @House is null))
    and
          (Postcode=isnull(@Postcode,Postcode) or (Postcode is null and @Postcode is null))

First bit of the conditional where is to use the variable, when present (the isnull bit is to ignore the variable if it's null)

Second bit of the conditional where is in case your evaluative field is null also as effectively fields don't = null they are 'is null'.

Confused? Good. Works on what I'm doing though!




回答7:


Here is my solution based on @Andomar answer above aimed at anyone testing an input varchar value, you need to test the parameter in the right order as per the example below:

FIELD1 = CASE
  WHEN @inputParameter = '' THEN FIELD1
  WHEN @inputParameter <> FIELD1 THEN NULL  -- if input is some text but does not match
  WHEN @inputParameter IS NULL THEN FIELD1
  WHEN @inputParameter != '' AND FIELD1 = @inputParameter THEN FIELD1
END

Hope this helps someone.



来源:https://stackoverflow.com/questions/8190114/using-case-statement-in-sql-with-parameter-variable-to-check-for-null-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!