FutureDBA-
asked on
picking data to the right or left of a character
How can i define a select statement to pick up based on data withing a colomun?
EG:
I have these results
I want to put everything left of a "-" or a " " in customer into chain, anything to the right of "-" or " " in customer, in customer.
eg;
current select statement
EG:
I have these results
CHAIN CUSTOMER ROUTE ITEM QTY SHIPDATE
7606-1570 30 2153 12 20140210
7606-1570 30 61100 12 20140210
7606-1570 30 61101 24 20140210
7606-1570 30 61102 12 20140210
7606-1570 30 61103 12 20140210
I want to put everything left of a "-" or a " " in customer into chain, anything to the right of "-" or " " in customer, in customer.
eg;
CHAIN CUSTOMER ROUTE ITEM QTY SHIPDATE
7606 1570 30 2153 12 20140210
7606 1570 30 61100 12 20140210
7606 1570 30 61101 24 20140210
7606 1570 30 61102 12 20140210
7606 1570 30 61103 12 20140210
current select statement
select
CASE WHEN SUBSTR(CUSTNO,1,1) in ('2','4') then '0' ELSE CHAIN END "CHAIN",
CUSTNO "CUSTOMER",
REP "ROUTE",
ITMNUM "ITEM",
QTY "QTY",
to_char(sysdate, 'yyyymmdd') SHIP_DATE
from credit_imports i;
are the portions you're interested in always 4 characters?
SELECT SUBSTR(custno, 1, 4) chain,
SUBSTR(custno, -4) customer,
rep "ROUTE",
itmnum "ITEM",
qty "QTY",
TO_CHAR(SYSDATE, 'yyyymmdd') ship_date
FROM credit_imports
SELECT SUBSTR(custno, 1, 4) chain,
SUBSTR(custno, -4) customer,
rep "ROUTE",
itmnum "ITEM",
qty "QTY",
TO_CHAR(SYSDATE, 'yyyymmdd') ship_date
FROM credit_imports
http:#a39848517 assumes your data is always numeric. If that's not the case and the data might not be 4 characters then try this..
SELECT REGEXP_SUBSTR(custno, '^[^- ]+') chain,
REGEXP_SUBSTR(custno, '[^- ]+$') customer,
rep "ROUTE",
itmnum "ITEM",
qty "QTY",
TO_CHAR(SYSDATE, 'yyyymmdd') ship_date
FROM credit_imports
SELECT REGEXP_SUBSTR(custno, '^[^- ]+') chain,
REGEXP_SUBSTR(custno, '[^- ]+$') customer,
rep "ROUTE",
itmnum "ITEM",
qty "QTY",
TO_CHAR(SYSDATE, 'yyyymmdd') ship_date
FROM credit_imports
ASKER
added more data.
When customer starts with 2 or 4, it should stay as is. another way to decipher if it's exactly 5 characters long like the 24568 example below
TABLE
select qry
current results
Expected results
When customer starts with 2 or 4, it should stay as is. another way to decipher if it's exactly 5 characters long like the 24568 example below
TABLE
CREATE TABLE CREDIT_IMPORTS
( XDATE VARCHAR2(20 BYTE),
REP VARCHAR2(200 BYTE),
ITMNUM VARCHAR2(20 BYTE),
QTY VARCHAR2(20 BYTE),
CUSTNO VARCHAR2(20 BYTE),
CHAIN VARCHAR2(20 BYTE),
STATUS VARCHAR2(3 BYTE)
)
Insert into CDC.CREDIT_IMPORTS (XDATE,REP,ITMNUM,QTY,CUSTNO,CHAIN,STATUS) values ('20140213','90','456','5','7603 15','0',null);
Insert into CDC.CREDIT_IMPORTS (XDATE,REP,ITMNUM,QTY,CUSTNO,CHAIN,STATUS) values ('20140213','90','456','5','7603-15','0',null);
Insert into CDC.CREDIT_IMPORTS (XDATE,REP,ITMNUM,QTY,CUSTNO,CHAIN,STATUS) values ('20140213','90','456','5','25468','0',null);
Insert into CDC.CREDIT_IMPORTS (XDATE,REP,ITMNUM,QTY,CUSTNO,CHAIN,STATUS) values ('20140213','90','456','5','1820 - 54','0',null);
Insert into CDC.CREDIT_IMPORTS (XDATE,REP,ITMNUM,QTY,CUSTNO,CHAIN,STATUS) values ('20140213','90','456','5','26451','0',null);
select qry
select
CHAIN,
CUSTNO,
REP,
ITMNUM,
QTY,
XDATE
from credit_imports;
current results
CHAIN CUSTNO REP ITEM QTY XDATE
0 7603 15 90 456 5 20140213
0 7603-15 90 456 5 20140213
0 24568 90 456 5 20140213
0 1820 - 54 90 456 5 20140213
0 26451 90 456 5 20140213
Expected results
CHAIN CUSTNO REP ITEM QTY XDATE
7603 15 90 456 5 20140213
7606 15 90 456 5 20140213
0 24568 90 456 5 20140213
1820 54 90 456 5 20140213
0 26451 90 456 5 20140213
ASKER
In the future you will get your answer a lot faster if you provide the exact requirements.
See if this works for you (I used sdstuber's regex just in case it isn't just numbers):
...
case when chain = '0' and substr(custno,1,1) in ('2','4') then chain else REGEXP_SUBSTR(custno, '^[^- ]+') end chain,
case when chain = '0' and substr(custno,1,1) in ('2','4') then custno else REGEXP_SUBSTR(custno, '[^- ]+$') end custno,
...
See if this works for you (I used sdstuber's regex just in case it isn't just numbers):
...
case when chain = '0' and substr(custno,1,1) in ('2','4') then chain else REGEXP_SUBSTR(custno, '^[^- ]+') end chain,
case when chain = '0' and substr(custno,1,1) in ('2','4') then custno else REGEXP_SUBSTR(custno, '[^- ]+$') end custno,
...
ASKER
it's on the right path, it fixes chain / cust when cust has a combo like '7620 - 190'
but when my chain is setup the correct way '7620' and custno 190.. I get 190 for both
Current Results using slightwv last suggestion
original data
I am looking for the data to stay the same since it is already in the format that I need it
but when my chain is setup the correct way '7620' and custno 190.. I get 190 for both
Current Results using slightwv last suggestion
190 190 100 3160 12 20140210
190 190 100 3161 12 20140210
original data
7620 190 100 3160 12 20140210
7620 190 100 3161 12 20140210
I am looking for the data to stay the same since it is already in the format that I need it
ASKER
i tried this exactly
WITH Q AS (select
case when chain = '0' and substr(custno,1,1) in ('2','4','7') then chain else REGEXP_SUBSTR(custno, '^[^- ]+') end chain,
case when chain = '0' and substr(custno,1,1) in ('2','4','7') then custno else REGEXP_SUBSTR(custno, '[^- ]+$') end custno,
REP "ROUTE",
ITMNUM "ITEM",
QTY "QTY",
to_char(sysdate, 'yyyymmdd') SHIP_DATE
from credit_imports)
select * from Q where custno like '%190%';
ASKER
I am also getting records like this
0 7606-1570 30 1031 12 20140210
0 7606-1570 30 1032 12 20140210
0 7606-1570 30 1203 12 20140210
0 7606-1570 30 1206 12 20140210
0 7606-1570 30 1208 12 20140210
0 7606-1570 30 1031 12 20140210
0 7606-1570 30 1032 12 20140210
0 7606-1570 30 1203 12 20140210
0 7606-1570 30 1206 12 20140210
0 7606-1570 30 1208 12 20140210
>>it's on the right path, it fixes chain / cust when cust has a combo like '7620 - 190'
>>but when my chain is setup the correct way '7620' and custno 190.. I get 190 for both
Please add actual insert statements to the sample above. Just 'guessing' I cannot reproduce the results you posted.
>>but when my chain is setup the correct way '7620' and custno 190.. I get 190 for both
Please add actual insert statements to the sample above. Just 'guessing' I cannot reproduce the results you posted.
ASKER
Please see attached file
ee1.sql
ee1.sql
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
for that row, chain 0, custno 26290.
testing now
testing now
ASKER
that looks good so far.
I will test in the AM with a full days worth of data. full day worth of data will process about 9k rows for this process.
thanks for the help. always appreciated.
I will test in the AM with a full days worth of data. full day worth of data will process about 9k rows for this process.
thanks for the help. always appreciated.
ASKER
Thank you sir, worked perfectly
...
regexp_substr(customer,'^[
regexp_substr(customer,'[0
...