2012-04-24 13 views
5

Ho provato alcune variazioni, ma dalla mia lettura della the documentation questo modello dovrebbe funzionare '' || val1 || val1 ... ma il mio risultato è una colonna vuota ...colonne CONCAT in PostgreSQL selezionare

thedb=# \d buildings_propertyvalue; 
           Table "public.buildings_propertyvalue" 
    Column |   Type   |        Modifiers        
-----------+------------------------+---------------------------------------------------------------------- 
id  | integer    | not null default nextval('buildings_propertyvalue_id_seq'::regclass) 
prop_id | integer    | not null 
place_id | integer    | not null 
float_val | double precision  | 
int_val | integer    | 
char_val | character varying(255) | 
text_val | text     | 

thedb=# select * from buildings_propertyvalue limit 10; 
id | prop_id | place_id | float_val | int_val | char_val | text_val 
-----+---------+----------+-----------+---------+----------+---------- 
798 |  3 |  170 |   |  831 |   | 
    2 |  46 |  180 |   |  0 |   | 
733 |  2 |  180 |  40 |   |   | 
737 |  10 |  180 |   |  0 |   | 
740 |  5 |  345 |  100 |   |   | 
742 |  10 |  345 |   |  0 |   | 
    11 |  2 |  170 |  50 |   |   | 
744 |  11 |  345 |   0 |   |   | 
746 |  14 |  345 |   |   | 52  | 
749 |  46 |  348 |   |  0 |   | 
(10 rows) 

thedb=# select prop_id, place_id, '' || float_val || int_val || char_val || text_val as val from buildings_propertyvalue limit 10; 
prop_id | place_id | val 
---------+----------+----- 
     3 |  170 | 
     46 |  180 | 
     2 |  180 | 
     10 |  180 | 
     5 |  345 | 
     10 |  345 | 
     2 |  170 | 
     11 |  345 | 
     14 |  345 | 
     46 |  348 | 
(10 rows) 

risposta

11

Concatenare un NULL con una stringa non vuota restituisce un NULL

Poiché le colonne *_val non sono valide, è probabilmente ciò che sta accadendo.

Prova questo:

'' || COALESCE(float_val::TEXT, '') || COALESCE(int_val::TEXT, '') || COALESCE(char_val, '') || COALESCE(text_val, '') 

o, se si può avere solo al massimo un valore non nullo, proprio questo:

COALESCE(float_val::TEXT, int_val::TEXT, char_val, text_val, '') 

Si noti che in PostgreSQL, a differenza di alcuni altri motori, TEXT has no downsides compared to VARCHAR . Non ha senso separare i dati TEXT e VARCHAR.

1

La concatenazione di un valore NULL con qualsiasi altro valore produce un valore NULL. Sembra che alcune delle colonne concatenate siano annullabili, quindi dovrai avvolgere una funzione COALESCE attorno a loro per forzare una stringa vuota o qualche altro valore segnaposto quando sono NULL.