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

RE: Consulta de select

From: "Hugo Gamarra" <hgamarra(at)hacienda(dot)gov(dot)py>
To: "'Luis Guevara'" <luisguevara(at)expressmail(dot)net>
Cc: "'PostgreSQL'" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: RE: Consulta de select
Date: 2005-03-31 14:27:29
Message-ID: 002201c535fd$c5a24030$ef06000a@SSET.GOV.PY (view raw, whole thread or download thread mbox)
Lists: pgsql-es-ayuda
Hola,  tal ves esto te sirva....

9.13.1. CASE
The SQL CASE expression is a generic conditional expression, similar to
if/else statements in other languages: 

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
ENDCASE clauses can be used wherever an expression is valid. condition
is an expression that returns a boolean result. If the result is true
then the value of the CASE expression is the result that follows the
condition. If the result is false any subsequent WHEN clauses are
searched in the same manner. If no WHEN condition is true then the value
of the case expression is the result in the ELSE clause. If the ELSE
clause is omitted and no condition matches, the result is null. 

An example: 



       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
    FROM test;

 a | case
 1 | one
 2 | two
 3 | other
The data types of all the result expressions must be convertible to a
single output type. See Section 10.5 for more detail. 

The following "simple" CASE expression is a specialized variant of the
general form above: 

CASE expression
    WHEN value THEN result
    [WHEN ...]
    [ELSE result]
ENDThe expression is computed and compared to all the value
specifications in the WHEN clauses until one is found that is equal. If
no match is found, the result in the ELSE clause (or a null value) is
returned. This is similar to the switch statement in C. 

The example above can be written using the simple CASE syntax: 

       CASE a WHEN 1 THEN 'one'
              WHEN 2 THEN 'two'
              ELSE 'other'
    FROM test;

 a | case
 1 | one
 2 | two
 3 | other
A CASE expression does not evaluate any subexpressions that are not
needed to determine the result. For example, this is a possible way of
avoiding a division-by-zero failure: 

SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
Saludos cordiales,
Hugo Gamarra.

> -----Mensaje original-----
> De: pgsql-es-ayuda-owner(at)postgresql(dot)org [mailto:pgsql-es-ayuda-
> owner(at)postgresql(dot)org] En nombre de Luis Guevara
> Enviado el: Jueves, 31 de Marzo de 2005 10:12
> Para: pgsql-es-ayuda(at)postgresql(dot)org
> Asunto: [pgsql-es-ayuda] Consulta de select
> Hola amigos:
> Tengo un campo con nombre FORMA que puede tener los valores: "1" o "0"
> ¿Como puedo hacer para que al ejecutar una consulta en lugar de 1
> "COPIA" y en lugar de 0 muetres "ORIGINAL"?
> Muchas gracias
> atte.
> Luis Guevara
> ---------------------------(fin del
> TIP 2: puedes desuscribirte de todas las listas simultáneamente
>     (envíe "unregister TuDirecciónDeCorreo" a

In response to

pgsql-es-ayuda by date

Next:From: Leonel NunezDate: 2005-03-31 14:28:03
Subject: Re: Consulta de select
Previous:From: Juan Pablo YañezDate: 2005-03-31 14:27:02
Subject: Problema de configuracion Local

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group