Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: SELECT de colonne alternative



Daniel Verite wrote:
	Pierre Couderc wrote:


Je voudrais selectionner une colonne, mais si elle est vide un autre colonne. Avec une fonvtion, cela pourrait donner :

SELECT testevaleur(length(col1)>0, col1, col2)...

ou testevaleur serait une fonction qui testerait si l'argument 1 est vrai, et rendrait suivant le cas l'argument 2 ou 3.


Ca peut s'écrire comme:

SELECT
 CASE WHEN length(col1)>0 THEN col1 ELSE col2 END,
...

Il existe également la fonction COALESCE

COALESCE(value [, ...])
Cette fonction retourne le premier argument non-null.

Ainsi,
select coalesce(col1,col2) from table ;
retournera col2 si col1 est null.



--
Stéphane SCHILDKNECHT
Président de PostgreSQLFr - http://www.postgresqlfr.org
ICQ : 142504394 - Yahoo! : stephanesas - IRC : SAS




Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group