简体   繁体   中英

Replacing Strings in Oracle 8i Query

I've got a table like this in ORACLE 8i

Value  | Timestamp
KKQ    | 10:00
KVKK   | 11:00
KMPE   | 12:00
PPKKPE | 13:00

and I need to replace KV for V , KM for M , PE for R , PP for N and P for L when querying these values.

What's the best way to do it? The problem I see is that we can have ANY combination of the strings in Value column... the values we can have there are: KK, Q, KV, V, KM, M, PE, R, PP, N, P, L .

select 
  replace(
  replace(
  replace(
  replace(<input>, 
    'KV', 'V'),
    'KM', 'M'),
    'PE', 'R'),
    'PP', 'N')
from
  ....

This cannot be solved by SQL, say with several REPLACE functions. The reason for this is that for instance PPP can mean PP-P or P-PP and thus be substituted either by PN or NP. Same for xxxKVxxx; is this KV or is it a trailing K and a leading V?

You will have to write a database function (PL/SQL) that loops through the string and replaces part by part. You will certainly know how to interpret PPP :-)

(BTW: Seems like a bad idea to store the classifications as a concatenated string of letters rather than in a separate table. You are using a relational database system without making use of the relational part. Hence the problem now.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM