Search code examples
sqlselectselect-query

Need to select a column value based on another column value if its true in sql?


Table structure

id  col1   col2
 1  data1  false
 2  data2  true

I tried

select id, case col2 when true then col1 from table

and it is showing an internal server error. I want to select the col1 from table when the corresponding col2 is true.


Solution

  • probably just a simple syntax error in your case statment. try this..

    select 
    id,
    case when col2=1 then col1 else 'some other value' end as computedCaseCol
    from table1
    

    see: SQL fiddle