SQL returns custom values ​​based on query results

You can make a query that returns another custom value based on the query. it’s hard to explain, here is an example that should make it more understandable.

what is in my table:

number

  • 1
  • 2
  • 3

here is what i need to return:

number

  • one
  • two
  • three

something like an if statement ...

+3
source share
2 answers

You need an operator case. Depending on your taste 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 
+3
source

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
+4
source

Source: https://habr.com/ru/post/1788026/


All Articles