how to use > = condition in sql case statement?

2019-08-11 19:27发布

问题:

I am trying to place a >= condition in statement, but it is not allowing this condition.

case 
   when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
   when ct.CRS_CAREER_LVL_CD = 'L'  then 'Law '
   when convert(int, left(ct.CRS_CATLG_NO, 4) > = 99 then 'Upper Division' 
   else 'Lower Division'
end as courseLevelName

Is there any other way to do this ?

回答1:

 when convert(int,left(ct.CRS_CATLG_NO,4)
             ^        ^                 ^           ^
            OPEN     OPEN              CLOSE      CLOSE ? 


回答2:

Guessing the problem is that the left 4 characters of are not always integers, if you're using SQL Server 2012 or newer you can use TRY_CONVERT():

case when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
     when ct.CRS_CAREER_LVL_CD = 'L'  then 'Law '
     when TRY_CONVERT(int,left(ct.CRS_CATLG_NO,4)) > = 99   then  'Upper Division' 
     else 'Lower Division'
end as courseLevelName

Edit: Looks like you were missing a closing ), if that wasn't a typo then that's likely the issue, if still getting an error then might be non-INT.