Difficult Substring Use

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

Here is an example of data and how long the data can be, either 9, 10, or 11, but no more, at the moment 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 following, then it would be that MONTH and the YEAR would be the next two letters, and DAY would default to the first day of the month. I need to display as a date because it is included in the 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) , , !!! contract_nm, (F.US.), 3 , Z11. 1 ( 9) 2 (10 ) 3 (11 ) , , . . 1 , , , 2 , 2- , "E" "A", "T" , , 1- . , 3 , 2 .

4) where, contract_nm, (F.US.), 3 , Z11. 1 ( 9) 2 (10 ) 3 (11 ) , , . . 2 , 2- , "E" "T" , , . , 3 , 3- , "E" "T" , , .

+3
3

:

1:

  contract_nm_month_year := substr(contract_nm, -3);

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;

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;

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;

.

+5

, CONTRACT_NM . , . .

, 5 , .

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) # 3

+4

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
+2
source

Source: https://habr.com/ru/post/1775144/


All Articles