简体   繁体   English

在Oracle 8i Query中替换字符串

[英]Replacing Strings in Oracle 8i Query

I've got a table like this in ORACLE 8i 我在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. 当我查询这些值时,我需要将KV替换为VKM替换为MPE替换为RPP替换为NP替换为L

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 . 我看到的问题是我们可以在Value列中有任意组合的字符串...我们可以拥有的值有: 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. SQL无法解决这个问题,比如几个REPLACE函数。 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. 其原因是例如PPP可以表示PP-P或P-PP,因此可以被PN或NP取代。 Same for xxxKVxxx; xxxKVxxx也是如此; is this KV or is it a trailing K and a leading V? 这是KV还是尾随K和领先的V?

You will have to write a database function (PL/SQL) that loops through the string and replaces part by part. 您必须编写一个循环遍历字符串的数据库函数(PL / SQL)并逐个替换。 You will certainly know how to interpret PPP :-) 你肯定会知道如何解释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.) (顺便说一句:将分类存储为串联的字母串而不是单独的表中似乎是一个坏主意。您使用的是关系数据库系统而不使用关系部分。因此现在出现问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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