Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
where子句中case-in中的SQL-between_Sql_Sql Server_Case_Where Clause_Between - Fatal编程技术网

where子句中case-in中的SQL-between

where子句中case-in中的SQL-between,sql,sql-server,case,where-clause,between,Sql,Sql Server,Case,Where Clause,Between,我尝试编写一个查询,当参数不为null时,返回param result和param-12 months之间的Date_列,或者当param为null时,返回Date_列不为null的数据。你能帮助我吗? 我尝试了许多变体,但总是有一些错误 declare @my_date DateTime select * from MyTable where case when @my_date is null then Date_column is not null

我尝试编写一个查询,当参数不为null时,返回param result和param-12 months之间的Date_列,或者当param为null时,返回Date_列不为null的数据。你能帮助我吗? 我尝试了许多变体,但总是有一些错误

declare @my_date DateTime
select * from MyTable
where 
case when @my_date is null 
           then Date_column is not null
           else Date_column between @my_date and DATEADD(MONTH, -12, @my_date)
           end

您可以像下面这样编写条件,而不使用
case
表达式

  where (Date_column is not null and  @my_date is null ) or 
     (Date_column between  DATEADD(MONTH, -12, @my_date) and @my_date)

您可以像下面这样编写条件,而不使用
case
表达式

  where (Date_column is not null and  @my_date is null ) or 
     (Date_column between  DATEADD(MONTH, -12, @my_date) and @my_date)

WHERE
子句中,通常最好使用
/
结构而不是
大小写
表达式。是的,你是对的。谢谢。通常最好使用
/
结构,而不是
WHERE
子句中的
case
表达式。是的,你是对的。谢谢。是的,很简单。是的,很简单。谢谢。