Skip to Main Content
  • Questions
  • I want to add an equation in text in established fields

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, SOUHEIL.

Asked: February 12, 2025 - 11:45 am UTC

Last updated: February 21, 2025 - 3:09 pm UTC

Version: ORACLE 19c

Viewed 1000+ times

You Asked

Hi,

I have a column in table that have yet text in fields. Some of fields have in the end of text: distance: R1: a R2: b R3: c mm mm mm
I want to insert before the first mm d/a, the second mm d/b and the third mm d/c. It will be : distance R1: a R2: b R3: c d/a mm d/b mm d/c mm

Please can you help me to write the update table code as update table set column = ??.

So example: in items we have R1: 43.5 R2: 44 R3: 45 mm mm mm. There are about 17000 row containing this informations. I want to introduce before mm a calculation: 337.5/43.5 = 7.76 mm for the first one. In final it will be R1: 43.5 R2: 44 R3: 45 7.76 mm 7.67 mm 7.5 mm.

I think perhaps I must use regexp_replace in update table:
update table set column = regexp_replace (column, ....);

The regexp_replace is very hard to understand. That is my problem.

Thanks.

and Chris said...

Which regex is hard to understand? Show us what you've tried!

You can do this without regexes though - assuming the string format is fixed. You can extract the Rx numeric values with a combination of substr & instr.

Use instr to find the location of an Rx value and the next to find the length of the number. Its position is the second parameter to substr. The difference between this at the next value, minus the character is the string length and the third argument to substr.

Here's an example that assigns the first two mm values to get you started:

select 
  rtrim ( replace ( s, 'mm' ) ) || ' ' || 
  round ( 337.5 / 
    substr ( s, 
      instr ( s, 'R1: ') + 4,
      instr ( s, 'R2: ') - instr ( s, 'R1: ') - 4
    ), 2
  ) || ' mm ' ||
  round ( 337.5 / 
    substr ( s, 
      instr ( s, 'R2: ') + 4,
      instr ( s, 'R3: ') - instr ( s, 'R2: ') - 4
    ), 2
  ) || ' mm '
from (
  select 'R1: 43.5 R2: 44 R3: 45 mm mm mm' s from dual
)

RTRIM(REPLACE(S,'MM'))||''||ROUND(337.5
---------------------------------------
R1: 43.5 R2: 44 R3: 45 7.76 mm 7.67 mm 

Rating

  (4 ratings)

Comments

Thank you works like a charm but

Souhei Mhiri, February 19, 2025 - 8:33 am UTC

Thank you for your help. It works in select statement but not in update statement. Invalid number error occurs. I've tried many solutions without success.
Chris Saxon
February 19, 2025 - 2:48 pm UTC

What exactly have you tried?

I’ve tried this

Souheil Mhiri, February 20, 2025 - 5:03 am UTC

update my table
set s = rtrim ( replace ( s, 'mm' ) ) || ' ' ||
round ( 337.5 /
substr ( s,
instr ( s, 'R1: ') + 4,
instr ( s, 'R2: ') - instr ( s, 'R1: ') - 4
), 2
) || ' mm ' ||
round ( 337.5 /
substr ( s,
instr ( s, 'R2: ') + 4,
instr ( s, 'R3: ') - instr ( s, 'R2: ') - 4
), 2
) || ' mm '
where s like ‘%R1:%’;

Chris Saxon
February 20, 2025 - 3:40 pm UTC

And what is the problem? What error did you get?

invalid number

Souheil Mhiri, February 21, 2025 - 2:55 pm UTC

The error was invalid number. With select it’s Ok, but when change it with update the error invalid number occurs.
Chris Saxon
February 21, 2025 - 3:09 pm UTC

Please provide a complete test case:

- create table
- insert into
- the statement that errors