Using tsql greater than sign in CASE Expression

前端 未结 6 2188
耶瑟儿~
耶瑟儿~ 2021-01-17 16:02
(case [dbo].[YearsInService]([DateEngaged],getdate())
   when (0) then (0) 
   when (1) then (4) 
   when (2) then (8)
    when (3) then (12)
     when (4) then (32)         


        
6条回答
  •  春和景丽
    2021-01-17 16:22

    Assuming (from your comment) that when DateEngaged is NULL, it causes YearsInService to be NULL, then I'd remove your current ELSE clause, and then use that for all other cases, something like:

    case COALESCE([dbo].[YearsInService]([DateEngaged],getdate()),-1)
    when (-1) then (-1)
    when (0) then (0) 
    when (1) then (4) 
    when (2) then (8)
    when (3) then (12)
     when (4) then (32) 
     when (5) then (40) 
     when (6) then (48) 
     when (7) then (56) 
     when (8) then (104) 
     when (9) then (117) 
     else (150) end
    

    If there's a concern about future-dated DateEngaged values, I'd deal with that inside of YearsInService rather than try to deal with it in the CASE expression.

提交回复
热议问题