Home » SQL & PL/SQL » SQL & PL/SQL » Oracle RDBMS equivalent for CONCAT_WS() - create CSV output (Oracle 11g)
Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619328] |
Mon, 21 July 2014 09:56  |
 |
rc3d
Messages: 213 Registered: September 2013 Location: Baden-Württemberg
|
Senior Member |
|
|
Hi
With CONCAT_WS it's possible to create a CSV with very less code
https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws
Quote:
CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.
mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name'
mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
I know in Oracle only the manual way
SELECT column1 || ',' || column2 || ',' ||.... FROM DUAL
is there a shortcut?
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619330 is a reply to message #619328] |
Mon, 21 July 2014 10:12   |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
SELECT concat(column1,',',column2,',',...) FROM table
You can also write your function. 
And with SQL*Plus, you can use "set colsep ','" then "select SELECT column1, column2, ... FROM table".
|
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619335 is a reply to message #619328] |
Mon, 21 July 2014 12:07   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
rc3d wrote on Mon, 21 July 2014 20:26
I know in Oracle only the manual way
SELECT column1 || ',' || column2 || ',' ||.... FROM DUAL
is there a shortcut?
I am afraid, if you are looking for a shortcut, and if you need to concatenate multiple strings, then CONCAT would just be more of coding in SQL. You would have to nest multiple CONCAT, since it allows only two arguments.
So, with the 2 options :
1. CONCAT
2. "||" operator
2nd option(concatenation operator) is easier to use when you have multiple strings(more than two).
Note : You need to take care of NULL in either of the cases.
Michel gave two good options, In SQL*Plus, colsep is an option. And another is to have a user defined function.
Regards,
Lalit
|
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619337 is a reply to message #619336] |
Mon, 21 July 2014 13:14   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Michel Cadot wrote on Mon, 21 July 2014 22:42
Quote:Note : You need to take care of NULL in either of the cases.
For what? Explain.
Ok, I will explain. OP said in his quote :
rc3d wrote on Mon, 21 July 2014 20:26
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
We know that Oracle considers NULL as an empty string, but in string concatenation, Oracle considers an empty string as NULL. So OP's statement "CONCAT_WS() does not skip empty strings" is not valid in Oracle.
SQL> select concat('hi','') from dual;
CO
--
hi
SQL> select concat('hi',null) from dual;
CO
--
hi
SQL> select concat(null,null) from dual
C
-
SQL> select concat('','')from dual
C
-
SQL>
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619338 is a reply to message #619337] |
Mon, 21 July 2014 13:31   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
And what does it have to do with creating CSV?
SELECT CONCAT('"',C1,'","',C2,'","',...,'"') FROM TBL
Is not dependent on NULLs. It is possible commas in column values that might be an issue. That's why I enclosed them in double quotes.
SY.
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619339 is a reply to message #619337] |
Mon, 21 July 2014 13:40   |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Given the sentence "With CONCAT_WS it's possible to create a CSV with very less code", || (and CONCAT for which I made an error in my first post as it only accepts 2 arguments and not more) works as expected (for this case) and could not work in an other way as it treats them as an empty string (what could it do?).
SQL> select '1' || ',' || null || ',' || '3' res from dual;
RES
----
1,,3
We have 3 fields with second one as null (empty).
Quote:So OP's statement "CONCAT_WS() does not skip empty strings" is not valid in Oracle.
Yes, it is as || does skip the NULL but
Quote:it does skip any NULL values
is not but can't be given that CONCAT_WS is more, for a single row multiple expressions) like LISTAGG (for a single expression on multiple rows) (as LISTAGG skips NULL) than like CONCAT or ||.
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619341 is a reply to message #619338] |
Mon, 21 July 2014 13:54   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Solomon Yakobson wrote on Tue, 22 July 2014 00:01
SELECT CONCAT('"',C1,'","',C2,'","',...,'"') FROM TBL
If I am not missing something in your post, it should throw "ORA-00909: invalid number of arguments", since CONCAT allows only 2 arguments.
SQL> create table t(a varchar2(10), b varchar2(10), c varchar2(10));
Table created.
SQL> insert into t select 'a', 'b', 'c' from dual;
1 row created.
SQL> select concat(a,',',b,',',c) from t;
select concat(a,',',b,',',c) from t
*
ERROR at line 1:
ORA-00909: invalid number of arguments
SQL> ed
Wrote file afiedt.buf
1* SELECT CONCAT('"',a,'","',b,'","',c,'"') FROM t
SQL> /
SELECT CONCAT('"',a,'","',b,'","',c,'"') FROM t
*
ERROR at line 1:
ORA-00909: invalid number of arguments
Coming back to the NULL, I pointed out to OP's statement about empty string and NULL, for string concatenation an empty string is considered as NULL in Oracle. I don't know about CONCAT_WS in MYSQL and how similar/different it is from Oracle.
|
|
|
Re: Oracle RDBMS equivalent for CONCAT_WS() - create CSV output [message #619342 is a reply to message #619339] |
Mon, 21 July 2014 14:04  |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Michel Cadot wrote on Tue, 22 July 2014 00:10
SQL> select '1' || ',' || null || ',' || '3' res from dual;
RES
----
1,,3
We have 3 fields with second one as null (empty).
Quote:So OP's statement "CONCAT_WS() does not skip empty strings" is not valid in Oracle.
Yes, it is as || does skip the NULL but
Quote:it does skip any NULL values
is not but can't be given that CONCAT_WS is more, for a single row multiple expressions) like LISTAGG (for a single expression on multiple rows) (as LISTAGG skips NULL) than like CONCAT or ||.
Perhaps I wanted to say the same thing as you, or you said the same thing which I tried to explain. To avoid any confusion, I will clarify myself a bit more, in your example which I have quoted, If I replace NULL with an empty string(let's say, not replace technically since Oracle considers it as NULL itself) :
SQL> select '1' || ',' || null || ',' || '3' res from dual;
RES
----
1,,3
SQL> ed
Wrote file afiedt.buf
1* select '1' || ',' ||''|| ',' || '3' res from dual
SQL> /
RES
----
1,,3
So, OP's statement "CONCAT_WS() does not skip empty strings" sounded confusing to me. In short, Oracle considers it to be NULL.
|
|
|
Goto Forum:
Current Time: Sun Apr 27 00:32:58 CDT 2025
|