Mani Pazhana
asked on
DB2 SQL
I have SQL column which holds the data as shown below
TYPE=A,CREATE USER=KWINTRODE ,MODIFIED USER=KWINTRODE ,2009 COUNTY TAXES
TYPE=N,CREATE USER=ZEKE ,MODIFIED USER=ZEKE ,ACH PAR NUMBER: 011280926
TYPE=C,CREATE USER=JWICK ,MODIFIED USER=JWICK ,ORECM-ELDER OHIO DELAWARE BUS TRUST LOAN 19700218
TYPE=A,CREATE USER=AMOORE ,MODIFIED USER=AMOORE ,Electric 2/7/11-3/7/11 & late fee
I want to write SQL to show the information below from the above data(bolded text):
A - 2009 COUNTY TAXES
N - ACH PAR NUMBER: 011280926
C - ORECM-ELDER OHIO DELAWARE BUS TRUST LOAN 19700218
A - Electric 2/7/11-3/7/11 & late fee
TYPE=A,CREATE USER=KWINTRODE ,MODIFIED USER=KWINTRODE ,2009 COUNTY TAXES
TYPE=N,CREATE USER=ZEKE ,MODIFIED USER=ZEKE ,ACH PAR NUMBER: 011280926
TYPE=C,CREATE USER=JWICK ,MODIFIED USER=JWICK ,ORECM-ELDER OHIO DELAWARE BUS TRUST LOAN 19700218
TYPE=A,CREATE USER=AMOORE ,MODIFIED USER=AMOORE ,Electric 2/7/11-3/7/11 & late fee
I want to write SQL to show the information below from the above data(bolded text):
A - 2009 COUNTY TAXES
N - ACH PAR NUMBER: 011280926
C - ORECM-ELDER OHIO DELAWARE BUS TRUST LOAN 19700218
A - Electric 2/7/11-3/7/11 & late fee
ASKER
Thanks
Here is my SQL:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE_IN_STRING(OBJECT, ',', 1, 2) + 1, 99999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
i am getting this error:
SQL0204N "*LIBL.LOCATE_IN_STRING" is an undefined name. SQLSTATE=42704
Here is my SQL:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE_IN_STRING(OBJECT, ',', 1, 2) + 1, 99999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
i am getting this error:
SQL0204N "*LIBL.LOCATE_IN_STRING" is an undefined name. SQLSTATE=42704
ASKER
i am using DB2 version 8.0
I think LOCATE_IN_STRING came with v9.
Before that you have to use LOCATE, but locate only finds you the first occurence... so you'll need to use it twice....
so instead of LOCATE IN STRING:
LOCATE(',',SUBSTRING(OBJEC T, LOCATE(',',OBJECT)+1,99999 ))
So you end up with:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC T, LOCATE(',',OBJECT)+1,99999 )) + 1, 99999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
hopefully this will work
Before that you have to use LOCATE, but locate only finds you the first occurence... so you'll need to use it twice....
so instead of LOCATE IN STRING:
LOCATE(',',SUBSTRING(OBJEC
So you end up with:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC
from LAWQA.L_HAPI
WHERE OBJECT IS not null
hopefully this will work
ASKER
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC
from LAWQA.L_HAPI
WHERE OBJECT IS not null
when i tried, i got this error:
SQL0138N The second or third argument of the SUBSTR function is out of range. SQLSTATE=22011
ASKER
i could run the SQL:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC T, LOCATE(',',OBJECT)+1,9999) ) + 1, 9999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
now it is returning the result:
C - ,MODIFIED USER=SMIRABELLA ,Account # 01309
C - ,MODIFIED USER=SMIRABELLA ,Account # 54194-20
A - ,MODIFIED USER=ZEKE , 610376179027820081025
i want my output like shown below:
C - Account # 01309
C - Account # 54194-20
A - 610376179027820081025
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC
from LAWQA.L_HAPI
WHERE OBJECT IS not null
now it is returning the result:
C - ,MODIFIED USER=SMIRABELLA ,Account # 01309
C - ,MODIFIED USER=SMIRABELLA ,Account # 54194-20
A - ,MODIFIED USER=ZEKE , 610376179027820081025
i want my output like shown below:
C - Account # 01309
C - Account # 54194-20
A - 610376179027820081025
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',SUBSTRING(OBJEC
from LAWQA.L_HAPI
WHERE OBJECT IS not null
still the same result:
C - ,MODIFIED USER=SMIRABELLA ,Account # 01309
C - ,MODIFIED USER=SMIRABELLA ,Account # 54194-20
A - ,MODIFIED USER=ZEKE , 610376179027820081025
A - ,MODIFIED USER=ZEKE , 717 761-5261 072 07Y20081025
A - ,MODIFIED USER=ZEKE , 717840109455520081025
Oh right, my error there's 3 ","
so last suggestion from momi_sabaq should work, he just added one more LOCATE to get the last ","
so last suggestion from momi_sabaq should work, he just added one more LOCATE to get the last ","
ASKER
please give me the SQL
Ok adding one more substring & locate gives:
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',substring(OBJEC T, LOCATE(',', SUBSTRING(OBJECT, LOCATE(',',OBJECT)+1,9999) ) + 1, 9999)) +1, 9999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',substring(OBJEC
from LAWQA.L_HAPI
WHERE OBJECT IS not null
ASKER
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',substring(OBJEC T, LOCATE(',', SUBSTRING(OBJECT, LOCATE(',',OBJECT)+1,9999) ) + 1, 9999)) +1, 9999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
it brings everything:
C - CREATE USER=SMIRABELLA ,MODIFIED USER=SMIRABELLA ,Account # 01309
C - CREATE USER=SMIRABELLA ,MODIFIED USER=SMIRABELLA ,Account # 54194-20
A - CREATE USER=ZEKE ,MODIFIED USER=ZEKE , 610376179027820081025
from LAWQA.L_HAPI
WHERE OBJECT IS not null
it brings everything:
C - CREATE USER=SMIRABELLA ,MODIFIED USER=SMIRABELLA ,Account # 01309
C - CREATE USER=SMIRABELLA ,MODIFIED USER=SMIRABELLA ,Account # 54194-20
A - CREATE USER=ZEKE ,MODIFIED USER=ZEKE , 610376179027820081025
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent. Thank you very much.
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',OBJECT,LOCATE(' ,',OBJECT, LOCATE(',',OBJECT)+1)+1) +1, 9999)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
Results:
C - Account # 01309
C - Account # 54194-20
A - 610376179027820081025
A - 717 761-5261 072 07Y20081025
A - 717840109455520081025
A - 319234884820081028
A - 319396221120081028
A - 319753549120081028
Select substring(OBJECT,6,1) || ' - ' || substring(OBJECT, LOCATE(',',OBJECT,LOCATE('
from LAWQA.L_HAPI
WHERE OBJECT IS not null
Results:
C - Account # 01309
C - Account # 54194-20
A - 610376179027820081025
A - 717 761-5261 072 07Y20081025
A - 717840109455520081025
A - 319234884820081028
A - 319396221120081028
A - 319753549120081028
ASKER
Thanks to both of you.
I don't understand why the query doesn't work, it should.. unless what looks like a comma is not a comma?
and for testing run:
Select LOCATE(',',OBJECT,LOCATE(' ,',OBJECT, LOCATE(',',OBJECT)+1)+1) ,
LOCATE(',',OBJECT, LOCATE(',',OBJECT)+1),
LOCATE(',',OBJECT)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
just to make sure each locate identifies correctly the location of each comma
and for testing run:
Select LOCATE(',',OBJECT,LOCATE('
LOCATE(',',OBJECT, LOCATE(',',OBJECT)+1),
LOCATE(',',OBJECT)
from LAWQA.L_HAPI
WHERE OBJECT IS not null
just to make sure each locate identifies correctly the location of each comma
ok, so without the many substrings it works, great.
ASKER
please take a look at it if you have time.
https://www.experts-exchange.com/questions/27416191/DB2-Sql.html
https://www.experts-exchange.com/questions/27416191/DB2-Sql.html
Sadly I'm not that familiar with DB2, and I don't have access to one to do testing, so can't really help with that other question.
from your_table