0

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'

Can someone help me with this.

Littlefoot
  • 131,892
  • 15
  • 35
  • 57
Dug
  • 1

1 Answers1

0

In Oracle, you can use:

SELECT TRIM(BOTH ',' FROM REGEXP_REPLACE(value, '\s+', ',')) AS replaced_value
FROM   table_name;

Which, for the sample data:

CREATE TABLE table_name (value) AS
SELECT ' mon tue wed thu ' FROM DUAL

Outputs:

REPLACED_VALUE
mon,tue,wed,thu

db<>fiddle here

MT0
  • 143,790
  • 11
  • 59
  • 117
  • I have a lot of data in the column like c1 = ' mon tue wed ' and c2 = ' mon tue wed thu fri ' and c3 = ' mon tue ' and so on... So how can i you use the same in a query? – Dug Jul 27 '22 at 18:52
  • Convert the solution about to a generic function – Pugzly Jul 27 '22 at 20:03
  • @Dug Just copy-paste the code for the different columns and change the column name. – MT0 Jul 27 '22 at 20:46