user3487430
user3487430

Reputation: 99

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.

Upvotes: 1

Views: 21376

Answers (2)

JohnHC
JohnHC

Reputation: 11195

Use a case statement

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

Upvotes: 5

juergen d
juergen d

Reputation: 204766

You can use a case

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

Upvotes: 0

Related Questions