TSQL- Using CASE in WHERE clause with a < or = sign

后端 未结 2 2041
别跟我提以往
别跟我提以往 2021-01-28 08:46

I am trying to select the IDs based on period and type of account.Each account type would have a different period. I tried the following code but it didnt like the < operator

相关标签:
2条回答
  • 2021-01-28 09:38

    You're misunderstanding the case statement. What you have in your THEN clause is what is returned by the CASE statement (like an if... then... statement). The way you have it written, it returns a boolean.

    Try this instead...

    SELECT ID, Period, Type
    FROM Table1
    WHERE Type='ASSET' AND Period < @inputperiod
    UNION
    SELECT ID, Period, Type
    FROM Table1
    WHERE Type='Liability' AND Period BETWEEN @start AND @enddate 
    
    0 讨论(0)
  • 2021-01-28 09:47

    You can't use CASE to swap out arbitrary bits of the query. You would need to use

       SELECT ID, Period, Type 
       FROM TABLE1
       WHERE (Type='ASSET' AND Period < @inputperiod ) 
        OR    (Type='Liability' AND Period BETWEEN @start AND @enddate)
    
    0 讨论(0)
提交回复
热议问题