4

We have varchar2(250) column that have various text data we want to replace with other specific text. Note: I do not want to use multiple replace function for this requirement, is there is any better way to do it

requirement is to:

Replace <   with    "less than"
Replace >   With    "greater than"
Replace &   With    and

For example data in table is:

"amount must be < 1 & > 2" 

Output of SQL should be:

"amount must be greater than 1 and less than 2" 
6
  • is that one column data ? Commented Feb 18, 2015 at 10:17
  • What's the problem with REPLACE when it is what meant for this? Commented Feb 18, 2015 at 10:20
  • Yes, this is one column data, for solution please use dual and sample text above Commented Feb 18, 2015 at 10:21
  • The problem with this is we need to replace 15 to 20 characters with string value Commented Feb 18, 2015 at 10:28
  • 1
    "I do not want to use multiple replace function" - why? What is the real problem you are trying to solve here? Commented Feb 18, 2015 at 11:10

3 Answers 3

1

According to me you have to use 3 replacement statements. Somthing like this:-

SELECT REPLACE( REPLACE( REPLACE ('amount must be < 1 & > 2', '<', 'less than'), '>' ,'greater than'), '&', 'and')
FROM DUAL;

I think this is only the solution.

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

Comments

1

You just need to use REPLACE thrice, that isn't a lot of effort.

SQL> WITH DATA AS(
  2  SELECT 'amount must be < 1 & > 2' str FROM dual
  3  )
  4  SELECT REPLACE(REPLACE(REPLACE(str, '<', 'less than'), '&','and'),'>','greater than') str
  5  FROM DATA
  6  /

STR
---------------------------------------------
amount must be less than 1 and greater than 2

SQL>

Comments

1

Use replace function

SELECT Replace(
          Replace(
            Replace(a, '>', 'greater than'), 
                              '<', 'lesser than'),
                                           '&', 'and')
FROM   Yourtable 

DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.