Skip to content

Instantly share code, notes, and snippets.

@xthiago
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xthiago/ff1cac758222b8385215 to your computer and use it in GitHub Desktop.
Save xthiago/ff1cac758222b8385215 to your computer and use it in GitHub Desktop.
SQL COALESCE - Retornar um valor ao invés de NULL

Ao invés de fazer uma querie enorme com CASE-THEN-ELSE, usar a função COALESCE. Ela retorna o primeiro argumento NOT NULL.

Isso:

SELECT CASE WHEN evento.realizado_em IS NULL THEN '' ELSE evento.realizado_em END FROM TableA

Vira isso:

SELECT COALESCE(evento.realizado_em, '') FROM TableA

Bem mais sucinto :)

PS: Testado no MySQL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment