一起使用INSTR和SUBSTR

时间:2010-11-29 17:27:31

标签: oracle substring

首先参见下面的数据,我需要如前所述从前2列计算出HUB_NM,PRODUCT_NM和STRIP_NM列。

DEAL_ORIGINATION EXCH_SYMBOL                                    HUB_NM     PRODUCT_NM            STRIP_NM
---------------- ---------------------------------------------- ---------- --------------------- ------------
TT_ICE           IPE e-Gas Oil DEC 2010                                    IPE e-Gas Oil         DEC 2010
GLOBEX           HO DEC 2010                                               HO                    DEC 2010
ICE NG           Firm Phys, ID, GDD - Transco-45 - Next Day Gas Transco-45 NG Firm Phys, ID, GDD Next Day Gas
STUSCO_ICE       Brent Crude Futures - North Sea - Dec12                   Brent Crude Futures   DEC12

我无法理解如何做到这一点。我知道我应该使用SUBSTR和INSTR,但我无法弄明白。

A)如何从EXCH_SYMBOL获取HUB_NM列值?

If T.DEAL_ORIGINATION =  'ICE'
then
    Find 1st space dash space 
    Find 2nd space dash space
    Display the word in between, no space at the end 
elsif T.DEAL_ORIGINATION in ('GLOBEX', 'TT_ICE', 'STUSCO_ICE')
then 
    null;
end if;

B)如何从EXCH_SYMBOL获取PRODUCT_NM列值?

If T.DEAL_ORIGINATION in ( 'ICE',  'STUSCO_ICE')
then
    Display from 1st character to the 1st dash, no space at the end 
elsif T.DEAL_ORIGINATION in ('GLOBEX', 'TT_ICE',)
then 
    Remove -9 caharacters from the end of the word and display the fornt word, no space at the end 
end if;

C)如何从EXCH_SYMBOL获取STRIP_NM列值?

If T.DEAL_ORIGINATION in ( 'ICE',  'STUSCO_ICE')
then
    Find the 2nd space dash space
    Display from then on to the end of the word, no space at the end 
elsif T.DEAL_ORIGINATION in ('GLOBEX', 'TT_ICE',)
then 
    Display the last -8 caharacters from the end of the word, no space at the end 
end if;

1 个答案:

答案 0 :(得分:0)

让我们开始添加一些指令来创建样本数据。

CREATE TABLE mytab
( 
DEAL_ORIGINATION VARCHAR2(100),
EXCH_SYMBOL VARCHAR2(100),
HUB_NM VARCHAR2(100),
PRODUCT_NM VARCHAR2(100),
STRIP_NM VARCHAR2(100)
);

INSERT INTO mytab (DEAL_ORIGINATION, EXCH_SYMBOL, HUB_NM, PRODUCT_NM, STRIP_NM) 
VALUES ('TT_ICE', 'IPE e-Gas Oil DEC 2010', null, 'IPE e-Gas Oil', 'DEC 2010' );

INSERT INTO mytab (DEAL_ORIGINATION, EXCH_SYMBOL, HUB_NM, PRODUCT_NM, STRIP_NM) 
VALUES ('GLOBEX', 'HO DEC 2010',null, 'HO', 'DEC 2010' );

INSERT INTO mytab (DEAL_ORIGINATION, EXCH_SYMBOL, HUB_NM, PRODUCT_NM, STRIP_NM) 
VALUES ('ICE NG','Firm Phys, ID, GDD - Transco-45 - NEXT DAY Gas', 'Transco-45', 'NG Firm Phys, ID, GDD', 'NEXT DAY Gas');
INSERT INTO mytab (DEAL_ORIGINATION, EXCH_SYMBOL, HUB_NM, PRODUCT_NM, STRIP_NM)
VALUES ('STUSCO_ICE', 'Brent Crude Futures - North Sea - Dec12', null, 'Brent Crude Futures', 'DEC12');

当然,你必须做很多工作来弄清楚instr和substr结果如何。此外,你永远不会想到或写下大量的括号。

我的建议是编写一个包含部分结果的临时选择指令,如下所示:

SELECT deal_origination, exch_symbol,
       INSTR(exch_symbol,' - ')+3 as string_start, 
       INSTR( SUBSTR(EXCH_SYMBOL,INSTR(exch_symbol,' - ')+3) , ' - ')-1 string_length ,
       SUBSTR(exch_symbol, INSTR(exch_symbol,' - ')+3, INSTR( SUBSTR(EXCH_SYMBOL,INSTR(exch_symbol,' - ')+3) , ' - ')-1 ) as RESULT
FROM mytab

请注意,RESULT列使用与string_start和string_length列相同的表达式。

这也回答了 A 问题

这将为您提供初始结果,因此您将能够弄清楚表达式中会发生什么。然后将所有内容放入DECODE指令

示例2:

decode ( DEAL_ORIGINATION, 
    'ICE', 'results in case of ICE',
    'GLOBEX', 'results in case of GLOBEX',
    null)
-- the last null is the default condition

最后要删除作品末尾的9个字符,请使用LENGTH函数

示例3:

-- this removes the last 6 characters from the hello world string
select substr ( 'hello world', 1, length('hello world) - 6 )

对于无法在实际计算机上测试Oracle代码而道歉。