Create comma delimited function [message #686325] |
Wed, 27 July 2022 14:39  |
 |
Unclefool
Messages: 65 Registered: August 2021
|
Member |
|
|
I would like to remove spaces and add commas in between the data using SQL. Like if i have column with the data ' mon tue wed thu ' i need it to be like 'mon,tue,wed,thu'
I got the following working but can this be converted into a generic function where I can call it on any column, like value1, value2…?
CREATE TABLE table_name (value) AS
SELECT ' mon tue wed thu ' FROM DUAL
SELECT TRIM(BOTH ',' FROM REGEXP_REPLACE(value, '\s+', ',')) AS replaced_value
FROM table_name;
|
|
|
|
|
|
Re: Create comma delimited function [message #686329 is a reply to message #686328] |
Thu, 28 July 2022 07:38   |
 |
Littlefoot
Messages: 21759 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
Just "convert" that SELECT statement into a function:
SQL> CREATE OR REPLACE FUNCTION f_space_to_comma (par_string IN VARCHAR2)
2 RETURN VARCHAR2
3 IS
4 BEGIN
5 RETURN TRIM (BOTH ',' FROM REGEXP_REPLACE (par_string, '\s+', ','));
6 END;
7 /
Function created.
Then call it wherever you want, e.g.
SQL> select f_space_to_comma(' mon tue wed thu ') result
2 from dual;
RESULT
--------------------------------------------------------------------------------
mon,tue,wed,thu
SQL> CREATE TABLE table_name (value) AS
2 SELECT ' mon tue wed thu ' FROM DUAL;
Table created.
SQL> select f_space_to_comma(value) result
2 from table_name;
RESULT
--------------------------------------------------------------------------------
mon,tue,wed,thu
SQL>
|
|
|
Re: Create comma delimited function [message #686330 is a reply to message #686329] |
Thu, 28 July 2022 08:31  |
 |
Michel Cadot
Messages: 68417 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
You give him no chance to learn and encourage his laziness, with his first post and previous topic (with no feedback by the way) he had the way to do it himself. 
Tell me and I'll forget; show me and I may remember; involve me and I'll understand
[Updated on: Thu, 28 July 2022 11:46] Report message to a moderator
|
|
|