Link to home
Start Free TrialLog in
Avatar of FutureDBA-
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
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

Open in new window



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

Open in new window



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;

Open in new window

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Try this:
...
regexp_substr(customer,'^[0-9]+') chain,
regexp_substr(customer,'[0-9]+$') customer,
...
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
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
Avatar of FutureDBA-

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

Open in new window


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);

Open in new window


select qry
select 
CHAIN,
CUSTNO,
REP,
ITMNUM,
QTY,
XDATE
from credit_imports;

Open in new window



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

Open in new window



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

Open in new window

also,

if chain has data value other than 0, leave untouched.. see below what happens.

good
User generated image

bad
User generated image
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,
...
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

190	190	100	3160	12	20140210
190	190	100	3161	12	20140210

Open in new window




original data
7620	190	100	3160	12	20140210
7620	190	100	3161	12	20140210

Open in new window


I am looking for the data to stay the same since it is already in the format that I need it
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%';

Open in new window

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
>>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.
Please see attached file
ee1.sql
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for that row, chain 0, custno 26290.

 testing now
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.
Thank you sir, worked perfectly