Skip to content

Instantly share code, notes, and snippets.

@abelcallejo
Last active August 31, 2018 06:18
Show Gist options
  • Save abelcallejo/190beb00a555b9f430ead1e11d2574ff to your computer and use it in GitHub Desktop.
Save abelcallejo/190beb00a555b9f430ead1e11d2574ff to your computer and use it in GitHub Desktop.
Using CASE-WHEN statement in PostgreSQL
/** Minimal example **/
SELECT
CASE
WHEN 1 > 0 THEN 'one is greater than zero'
ELSE 'something else'
END AS "ALIASED_FIELD_NAME_HERE"
/** In table example **/
SELECT
CASE
WHEN table_name.field_name::integer > 0 THEN 'it is more than zero'
WHEN table_name.field_name::integer = 1 THEN 'it is single'
ELSE 'something else'
END AS "ALIASED_FIELD_NAME_HERE"
FROM
table_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment