![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a series of VARCHAR2 columns that might be null or not. Is there a simple way to concatenate them using a separator only between non-null values? E.g.: COUNTRY CITY * *DISTRICT STREET ======= ======= ======== ========== Spain * Madrid *Chueca Ukrania UK * * *London * * * * * Oxford St. I'd like: Spain; Madrid; Chueca Ukrania UK; London; Oxford St. ... rather than: Spain; Madrid; Chueca; Ukrania; ; ; UK; London; ; Oxford St. I'm running Oracle9i Enterprise Edition Release 9.2.0.1.0. Thank you in advance. -- --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programación web:http://bits.demogracia.com -- Mi web de humor al baño María:http://www.demogracia.com -- |
#3
| |||
| |||
|
|
SQL> select concat_add 2 from 3 (select country||';'||city||';'||street concat_add, rownum rn 4 from location 5 where country is not null 6 and city is not null 7 and street is not null 8 union 9 select country||';'||city concat_add, rownum rn 10 from location 11 where country is not null 12 and city is not null 13 and street is null 14 union 15 select country||';;'||street concat_add, rownum rn 16 from location 17 where country is not null 18 and street is not null 19 and city is null 20 union 21 select country concat_add, rownum rn 22 from location 23 where city||street is null) 24 order by rn; [...] There may be a more elegant solution, I simply don't have any more time to work on this. |
#4
| |||
| |||
|
|
ddf escribió: SQL> select concat_add * 2 *from * 3 *(select country||';'||city||';'||street concat_add, rownum rn * 4 *from location * 5 *where country is not null * 6 *and city is not null * 7 *and street is not null * 8 *union * 9 *select country||';'||city concat_add, rownum rn *10 *from location *11 *where country is not null *12 *and city is not null *13 *and street is null *14 *union *15 *select country||';;'||street concat_add, rownum rn *16 *from location *17 *where country is not null *18 *and street is not null *19 *and city is null *20 *union *21 *select country concat_add, rownum rn *22 *from location *23 *where city||street is null) *24 *order by rn; [...] There may be a more elegant solution, I simply don't have any more time to work on this. My own workaround for a specific application I just had to finish ASAP was an chaotic series of CASE ... END statements that would scare you away. That's why I'm longing for something more, well, elegant. Thank you for your time. -- --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programación web:http://bits.demogracia.com -- Mi web de humor al baño María:http://www.demogracia.com --- Hide quoted text - - Show quoted text - |
#5
| ||||
| ||||
|
|
On Feb 4, 12:03 pm, "Álvaro G. Vicario" alvaro.NOSPAMTH... (AT) demogracia (DOT) com> wrote: I have a series of VARCHAR2 columns that might be null or not. Is there a simple way to concatenate them using a separator only between non-null values? |
|
And you're very welcome, it's an enjoyable exercise. |
|
| case when city is null then '' else city || ';' end | case when district is null then '' else district || ';' end | case when street is null then '' else street end , ';') csv |
|
| decode(city , null, '', city || ';') | decode(district, null, '', district || ';') | decode(street , null, '', street) , ';') csv |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
I have a series of VARCHAR2 columns that might be null or not. Is there a simple way to concatenate them using a separator only between non-null values? I am bit irritated by this requirement: with this approach you won't be able to tell from which DB field a value came. If you do a CSV import in some other database or other tool, values from the same column will not properly align. Is this really what you want? |
|
:-) Another approach using good old CASE and RTRIM: select rtrim( case when country is null then '' else country || ';' end || case when city is null then '' else city || ';' end || case when district is null then '' else district || ';' end || case when street is null then '' else street end , ';') csv from location |
![]() |
| Thread Tools | |
| Display Modes | |
| |