开发者

Tricky use of substring

开发者 https://www.devze.com 2023-01-25 02:23 出处:网络
My question is about a field called contract_nm varchar2(14). I need to get 3 different values from it use the field to get the filter for a where clause.

My question is about a field called contract_nm varchar2(14). I need to get 3 different values from it use the field to get the filter for a where clause.

Here is the sample data and how long the data might be, either 9 or 10 or 11, no more for now it can be.

CONTRACT_NM               LENGTH(CONTRACT_NM)
F.US.WZ10                          9
F.US.WZ11                          9
F.US.WZ12                          9
F.US.RBZ10                         10
F.US.RBZ11                         10
F.US.RBZ12                         10
F.US.ZWAZ10                     11
F.US.ZWAZ11                     11
F.US.ZWAZ12                     11
etc

1) I need to display the last 3 characters of contract_nm.

2) Check the last 3 characters of contract_nm to see if the first letter is one of the below, then it would be that MONTH and the YEAR would be the next 2 letters and the DAY would be defaulted to the first day of the month. I need to display like a date because it is going into a date field.

Trade Months (Terms):
F              January                                
G             February                              
H             March                   
J              April                       
K             May
M       June                           
N             July
Q             August
U             September
V             October
X             November
Z       December

3) This is a bit difficult to explain in writing, I will try and hope you understand me!!! Read contract_nm data, ignore the first five characters (F.US.), then ignore the last 3 characters i.e. Z11. Now, what开发者_运维百科 we are working with is either 1 character (9 length) or 2 characters (10 length) or 3 characters (11 length) of data in the middle, always, see the below data. If 1 character, then display that letter, else if 2 characters, then check the 2nd letter if is not ‘E’ or ‘A’ or ‘T’ then display both letters, else display just the 1st letter. Else if 3 characters, then display the first 2 letters.

4) I need a filter for my where clause to read contract_nm data, ignore the first five characters (F.US.), then ignore the last 3 characters i.e. Z11. Now, what we are working with is either 1 character (9 length) or 2 characters (10 length) or 3 characters (11 length) of data in the middle, always, see the below data. If 2 characters, then check the 2nd letter if is not ‘E’ or ‘T’ then don’t pull the data, else do it. Else if 3 characters, then check the 3rd letter if is not ‘E’ or ‘T’ then don’t pull the data, else do it.


Try the following:

Question 1:

  contract_nm_month_year := substr(contract_nm, -3);

Question 2:

  contract_date := CASE substr(contract_nm, -3, 1)
                      WHEN 'F' THEN TO_DATE('01-JAN-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'G' THEN TO_DATE('01-FEB-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'H' THEN TO_DATE('01-MAR-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'J' THEN TO_DATE('01-APR-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'K' THEN TO_DATE('01-MAY-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'M' THEN TO_DATE('01-JUN-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'N' THEN TO_DATE('01-JUL-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'Q' THEN TO_DATE('01-AUG-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'U' THEN TO_DATE('01-SEP-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'V' THEN TO_DATE('01-OCT-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'X' THEN TO_DATE('01-NOV-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      WHEN 'Z' THEN TO_DATE('01-DEC-' || SUBSTR(contract_nm, -2), 'DD-MON-RR');
                      ELSE NULL;
                    END;

Question 3:

  display_3 := CASE length(contract_nm)
                 WHEN 9 THEN SUBSTR(contract_nm, 6, 1);
                 WHEN 10 THEN
                    CASE SUBSTR(contract_nm, 7, 1)
                      WHEN 'E' THEN SUBSTR(contract_nm, 6, 1);
                      WHEN 'A' THEN SUBSTR(contract_nm, 6, 1);
                      WHEN 'T' THEN SUBSTR(contract_nm, 6, 1);
                      ELSE SUBSTR(contract_nm, 6, 2);
                    END;
                 WHEN 11 THEN SUBSTR(contract_nm, 6, 2)
               END;

Question 4:

  WHERE CASE length(contract_nm)
          WHEN 9 THEN 0 -- never pull data for contract length = 9
          WHEN 10 THEN
            CASE SUBSTR(contract_nm, 7, 1)
              WHEN 'E' THEN 1
              WHEN 'T' THEN 1
              ELSE 0
            END
          WHEN 11 THEN
            CASE SUBSTR(contract_nm, 8, 10
              WHEN 'E' THEN 1
              WHEN 'T' THEN 1
              ELSE 0
            END
          ELSE 0
        END = 1;

Share and enjoy.


Focus on how to break CONTRACT_NM up into the pieces you need. Then create a view so that you can treat each piece as a column. Applying your various conditions and transformations should then be straightforward.

It appears you are always ignoring the first 5 characters, so just chop them out.

SELECT SUBSTR( contract_nm, 5, LENGTH(contract_nm)-8 ) flags,
       SUBSTR( contract_nm, -3, 1 ) month_flag,
       SUBSTR( contract_nm, -2 ) year
  FROM table

1) month_flag||year.
2)

TO_DATE(
        CASE month_flag WHEN 'F' THEN 'JAN' WHEN 'G' THEN 'FEB' ... END || year,
        'MONRR'
        )

3)

CASE WHEN LENGTH(flags) = 3
          THEN SUBSTR(flags,1,2)
        WHEN LENGTH(flags) = 2 AND SUBSTR(flags,2,1) NOT IN ('E','A','T')
          THEN flags
        ELSE SUBSTR(flags,1,1)

4) Very similar to #3


1) I need to display the last 3 characters of contract_nm.

Use SUBSTR(contract_nm, -3)

Re Q2, use a CASE expression something like:

CASE SUBSTR(contract_nm, -3, 1)
  WHEN 'F' THEN TO_DATE('JAN'||SUBSTR(contract_nm, -2), 'MONRR')
  WHEN 'G' THEN TO_DATE('FEB'||SUBSTR(contract_nm, -2), 'MONRR')
  ...etc.
  END
0

精彩评论

暂无评论...
验证码 换一张
取 消