![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
SELECT s_house,s_post_dir,s_street,s_suffix FROM parcels WHERE s_pin = '1201703303520'; s_house | s_post_dir | s_street | s_suffix ---------+------------+----------------+---------- 34643 | | FIG TREE WOODS | So to get "34643 FIG TREE WOODS" what do I do ? SELECT s_house || ' ' || s_post_dir || ' ' || s_street || ' ' || s_suffix FROM parcels WHERE s_pin = '1201703303520'; ?column? ---------- (1 row) I have tried all manner of COALESCE and various trickeries. Nothing works. In Informix this works exactly as I think it should. Is Informix totally whack, or what ? |
#3
| |||
| |||
|
|
This is postgres 7.4 on a linux box ... I have driven myself to distraction trying to what ought to be easy. I have a table with house number, street direction, street name and street suffix as 4 columns. I want to paste them together as one text string for use by another application. SELECT s_house,s_post_dir,s_street,s_suffix FROM parcels WHERE s_pin = '1201703303520'; s_house | s_post_dir | s_street | s_suffix ---------+------------+----------------+---------- 34643 | | FIG TREE WOODS | So to get "34643 FIG TREE WOODS" what do I do ? SELECT s_house || ' ' || s_post_dir || ' ' || s_street || ' ' || s_suffix FROM parcels WHERE s_pin = '1201703303520'; ?column? ---------- (1 row) I have tried all manner of COALESCE and various trickeries. Nothing works. In Informix this works exactly as I think it should. Is Informix totally whack, or what ? I guess I will have to code a perl script to do this seemingly straightforward operation. Any suggestions as to what i am missing (and I've been back and forth through the manual) would be most welcome. |
#4
| |||
| |||
|
|
This is postgres 7.4 on a linux box ... I have driven myself to distraction trying to what ought to be easy. I have a table with house number, street direction, street name and street suffix as 4 columns. I want to paste them together as one text string for use by another application. SELECT s_house,s_post_dir,s_street,s_suffix FROM parcels WHERE s_pin = '1201703303520'; s_house | s_post_dir | s_street | s_suffix ---------+------------+----------------+---------- 34643 | | FIG TREE WOODS | So to get "34643 FIG TREE WOODS" what do I do ? SELECT s_house || ' ' || s_post_dir || ' ' || s_street || ' ' || s_suffix FROM parcels WHERE s_pin = '1201703303520'; ?column? ---------- (1 row) I have tried all manner of COALESCE and various trickeries. Nothing works. In Informix this works exactly as I think it should. Is Informix totally whack, or what ? I guess I will have to code a perl script to do this seemingly straightforward operation. Any suggestions as to what i am missing (and I've been back and forth through the manual) would be most welcome. |
#5
| |||
| |||
|
|
SELECT s_house,s_post_dir,s_street,s_suffix FROM parcels WHERE s_pin = '1201703303520'; s_house | s_post_dir | s_street | s_suffix ---------+------------+----------------+---------- 34643 | | FIG TREE WOODS | So to get "34643 FIG TREE WOODS" what do I do ? SELECT s_house || ' ' || s_post_dir || ' ' || s_street || ' ' || s_suffix FROM parcels WHERE s_pin = '1201703303520'; ?column? ---------- |
#6
| |||
| |||
|
|
This is postgres 7.4 on a linux box ... I have driven myself to distraction trying to what ought to be easy. I have a table with house number, street direction, street name and street suffix as 4 columns. I want to paste them together as one text string for use by another application. SELECT s_house,s_post_dir,s_street,s_suffix FROM parcels WHERE s_pin = '1201703303520'; s_house | s_post_dir | s_street | s_suffix ---------+------------+----------------+---------- 34643 | | FIG TREE WOODS | So to get "34643 FIG TREE WOODS" what do I do ? SELECT s_house || ' ' || s_post_dir || ' ' || s_street || ' ' || s_suffix FROM parcels WHERE s_pin = '1201703303520'; ?column? ---------- (1 row) I have tried all manner of COALESCE and various trickeries. Nothing works. In Informix this works exactly as I think it should. Is Informix totally whack, or what ? I guess I will have to code a perl script to do this seemingly straightforward operation. Any suggestions as to what i am missing (and I've been back and forth through the manual) would be most welcome. Thanks, Greg Williamson DBA (hah!) GlobeXplorer LLC ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives? http://archives.postgresql.org |
#7
| |||
| |||
|
|
SELECT TRIM(TRIM(TRIM(COALESCE(s_house,'') || ' ' || COALESCE(s_post_dir,'')) || ' ' || COALESCE(s_street ,'')) || ' ' || COALESCE(s_suffix,'')) FROM parcels WHERE s_pin = '1201703303520'; The TRIMs are to remove surplus spaces from inside the result string. Avoiding the inner trims: |
|
| COALESCE(s_post_dir || ' ','') | COALESCE(s_street || ' ','') | COALESCE(s_suffix,'') ) FROM parcels WHERE s_pin = '1201703303520'; |
| ---------------------------(end of broadcast)--------------------------- TIP 6: Have you searched our list archives? http://archives.postgresql.org |
#8
| |||
| |||
|
|
[snip] SELECT TRIM(TRIM(TRIM(COALESCE(s_house,'') || ' ' || COALESCE(s_post_dir,'')) || ' ' || COALESCE(s_street ,'')) || ' ' || COALESCE(s_suffix,'')) FROM parcels WHERE s_pin = '1201703303520'; The TRIMs are to remove surplus spaces from inside the result string. Avoiding the inner trims: SELECT TRIM( COALESCE(s_house || ' ','') || COALESCE(s_post_dir || ' ','') || COALESCE(s_street || ' ','') || COALESCE(s_suffix,'') ) FROM parcels WHERE s_pin = '1201703303520'; Looks a bit more understandable :-) |
#9
| |||
| |||
|
|
But it's still too cumbersome. How about creating a new operator? With the example below the query would simply be SELECT s_host ||~ s_post_dir ||~ s_street ||~ s_suffix FROM parcels WHERE s_pin = '1201703303520'; alvherre=# CREATE FUNCTION text_concat_nulls_with_an_embedded_space(text, text) RETURNS text AS 'SELECT CASE WHEN $1 IS NULL THEN $2 WHEN $2 IS NULL THEN $1 ELSE $1 || '' '' || $2 END' LANGUAGE sql; CREATE FUNCTION alvherre=# SELECT text_concat_nulls_with_an_embedded_space('foo', NULL); text_concat_nulls_with_an_embedded_space ------------------------------------------ foo (1 fila) alvherre=# SELECT text_concat_nulls_with_an_embedded_space(NULL, 'foo'); text_concat_nulls_with_an_embedded_space ------------------------------------------ foo (1 fila) alvherre=# SELECT text_concat_nulls_with_an_embedded_space('bar', 'foo'); text_concat_nulls_with_an_embedded_space ------------------------------------------ bar foo (1 fila) alvherre=# CREATE OPERATOR ||~ (PROCEDURE = text_concat_nulls_with_an_embedded_space, LEFTARG = text, RIGHTARG = text); CREATE OPERATOR alvherre=# SELECT 'hi' ||~ 'foo' ||~ null ||~ null ||~ 'bar' ||~ 'baz' ||~ null ||~ 'bye'; ?column? -------------------- hi foo bar baz bye (1 fila) |
#10
| |||
| |||
|
|
[snip] SELECT TRIM(TRIM(TRIM(COALESCE(s_house,'') || ' ' || COALESCE(s_post_dir,'')) || ' ' || COALESCE(s_street ,'')) || ' ' || COALESCE(s_suffix,'')) FROM parcels WHERE s_pin = '1201703303520'; The TRIMs are to remove surplus spaces from inside the result string. Avoiding the inner trims: SELECT TRIM( COALESCE(s_house || ' ','') || COALESCE(s_post_dir || ' ','') || COALESCE(s_street || ' ','') || COALESCE(s_suffix,'') ) FROM parcels WHERE s_pin = '1201703303520'; Looks a bit more understandable :-) |
![]() |
| Thread Tools | |
| Display Modes | |
| |