SQL CASE WHEN …AND

夙愿已清 提交于 2019-12-13 19:23:53

问题


Is there a way to write a SQL query like this:

CASE WHEN DATEPART(yy,@year_end) = 2013 AND DATEPART(mm,@year_end) = 3         
        THEN @AdjStartYear = '2012/07/01' AND @AdjEndYear = '2012/12/31'

Basically, I want 2 things to happen after the CASE statement is evaluated.

Thanks


回答1:


No, you have to make 2 case statements

select @AdjStartYear = CASE WHEN DATEPART(yy,@year_end) = 2013 AND DATEPART(mm,@year_end) = 3
                            THEN  '2012/07/01' 
                       END,
       @AdjEndYear = CASE WHEN DATEPART(yy,@year_end) = 2013 AND DATEPART(mm,@year_end) = 3
                          THEN  '2012/12/31' 
                     END



回答2:


You could do it without the CASE statement:

SELECT @AdjStartYear = '2012/07/01', @AdjEndYear = '2012/12/31'
WHERE DATEPART(year, @year_end) = 2013 AND DATEPART(month, @year_end) = 3

This only performs the operation if the WHERE criteria is met. sqlFiddle



来源:https://stackoverflow.com/questions/24254164/sql-case-when-and

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