0

I want to remove the space after comma, in a column using SQL script. I am using oracle DB.

For example, DB structure

I want a space (after sathish, kumar, raghavan) to be removed from column 'name',

I want the table to look like,Required format

likewise all the records to be modified. I tried, this query,

update incitable SET name = LTRIM(RTRIM(name));

But it didnt make any chages,

Please help on this.

Sorry for the grammar mistakes.

2
  • 1
    Why are you storing lists of values in a single column? Assuming you're stuck with that data model, is it always exactly one space after the comma, or can there be several, or other whitespace like tabs? Commented Jun 1, 2017 at 14:12
  • No each comma separated value has a role to play in a single dropdown in the front end. The angular grid in my front end do not accept space or tab space. Commented Jun 2, 2017 at 6:04

1 Answer 1

5

Use replace():

UPDATE incitable
    SET name = REPLACE(name, ', ', ',');

TRIM() removes spaces at the beginning and end of the string, but not spaces in-between. Given that TRIM() is available, there is no need to use LTRIM() and RTRIM() (unless you have a fetish for SQL Server).

Note: Storing lists of values in a single column is a really, really bad idea. Oracle offers many other solutions, such as a junction table (the SQLish way to store lists), nested tables, JSON formats, and no doubt more methods.

Sign up to request clarification or add additional context in comments.

3 Comments

But will it work with values like "sss, safa, aafa, agag, agga" which has more number of sub string.
@s.sathishkumar . . . It will work on any number of commas in the string.
@UdayShankar . . . That is not the question being asked here. If you have another question, ask it as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.