How do I do multiple CASE WHEN conditions using SQL Server 2008?

前端 未结 10 1959
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 09:57

What I\'m trying to do is use more than one CASE WHEN condition for the same column.

Here is my code for the query:

   SELECT   Url=\'\',
                    


        
10条回答
  •  清酒与你
    2020-11-27 10:38

    I had a similar but it was dealing with dates. Query to show all items for the last month, works great without conditions until Jan. In order for it work correctly, needed to add a year and month variable

    declare @yr int
    declare @mth int
    
    set @yr=(select case when month(getdate())=1 then YEAR(getdate())-1 else YEAR(getdate())end)
    set @mth=(select case when month(getdate())=1 then month(getdate())+11 else month(getdate())end)
    

    Now I just add the variable into condition: ...

    (year(CreationTime)=@yr and MONTH(creationtime)=@mth)
    

提交回复
热议问题