Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL returning custom values based on query results

Tags:

sql

sql-server

is it possible to make a query that returns a different custom value based on a query. its hard to explain, here is an example that should make it more clear.

what I have in the table:

number

  • 1
  • 2
  • 3

here is what I want returned:

number

  • one
  • two
  • three

something like an if statement...

like image 813
ajoe Avatar asked Jan 26 '11 20:01

ajoe


2 Answers

You want a case statement. Depending on your flavor of SQL, something like this should work:

select 
    bar = case 
               when foo = 1 then 'one'
               when foo = 2 then 'two'
               else 'baz' 
          end
from myTable 
like image 60
Chase Avatar answered Oct 20 '22 00:10

Chase


Try

select value = case t.value
               when 1 then 'one'
               when 2 then 'two'
               when 3 then 'three'
               ...
               else null
               end
from my_table t
like image 27
Nicholas Carey Avatar answered Oct 20 '22 00:10

Nicholas Carey