简体   繁体   中英

IF and ELSE Statement based on column value

Based on one column within my query results (Value), I am trying to write an if/else statement based on the value held which will display the result the in an additional row.

For example, if I have a record of 2 within the value field, but I want to check whether it is above < 5. If the value is less than 5 I basically want the additional column to display a hardcoded value of 5, else display actual value.

Any help would be appreciated.

Use a case statement

select a.*, 
       case
           when a.TheField < 5 then 5
           else a.TheField
       end as NewField
from MyTable a

You can use a case

select value, case when value < 5 
                   then 5 
                   else value 
              end as calculated_column
from your_table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM