1

I want to replace spaces between words with a single underscore. I can't use the replace_regex Oracle function to do this, because it is not supported by the converter which we are using to convert sql statements so that they will be independent of platform such as SQL Server, PostgreSQL etc. .

Currently converter is providing support for 'substr' , 'replace', and 'instr' oracle functions. how can i use these three functions to get below output. Example:

"my name is xyz" => "my_name_is_xyz"
"test sdf" => "test_sdf"
Laurenz Albe
61.9k4 gold badges57 silver badges93 bronze badges
asked Apr 22, 2020 at 21:26
1
  • 1
    Why Oracle and SQL-Server both are tagged? I guess its Oracle question. Commented Apr 23, 2020 at 5:39

1 Answer 1

1

Something like this might work for you, at least if replace does the same on all your platforms as in SQL Server.

  1. Replace all spaces with space+underscore
  2. Replace underscore+space with an empty string
  3. Replace space with an empty string

replace(replace(replace('my name is xyz', ' ', '_ '), ' _', ''), ' ', '');
answered Apr 23, 2020 at 5:57
2
  • It won't work correctly for 'a_ b', but maybe this is ok for the OP. Otherwise I think substituting all underscore+blank by underscore+underscore+blank before one proceeds with yor steps may help. Commented Apr 23, 2020 at 6:36
  • @miracle173 Sure, depends on what you want. It is unclear in your example if the already existing underscore should be treated as part of a word or as an already existing delimiter. Only OP knows. BTW, these kind of string manipulation things gets complicated fast if you choose to use a special character (underscore in this case) that may already exist in the string you are manipulating. Commented Apr 23, 2020 at 6:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.