SELECT REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 2) AS VAL
FROM
(
SELECT 'CAT,DOG,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS MYCOL FROM DUAL
);
Output would be - DOG
to get 4th value just change
REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 4)
Related Links:
- Function to Compare two Comma Separated Strings in Oracle
- Oracle: New String Aggregation Techniques
- Oracle: Difference between wm_concat and ListAgg
- Oracle: Comma Separated String To Rows 11g
- Oracle: Comma Separated String To Rows 10g Onwards
- Oracle: Department wise Employee Ids Comma Separated
- Oracle: Comma Separated String To Rows
FROM
(
SELECT 'CAT,DOG,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS MYCOL FROM DUAL
);
Output would be - DOG
to get 4th value just change
REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 4)
Related Links:
- Function to Compare two Comma Separated Strings in Oracle
- Oracle: New String Aggregation Techniques
- Oracle: Difference between wm_concat and ListAgg
- Oracle: Comma Separated String To Rows 11g
- Oracle: Comma Separated String To Rows 10g Onwards
- Oracle: Department wise Employee Ids Comma Separated
- Oracle: Comma Separated String To Rows
thanx its really good :)
ReplyDeleteit's really helpful.
ReplyDeleteSome times we need to handle comma separated strings.
Thanks
Dont forget to check related links of this post !!!
DeleteThis is useful. My algortimic function library entry
ReplyDeleteFUNCTION EXTRACTELEMENT(p_Txt VARCHAR2, pos_n integer, p_Separator VARCHAR2) RETURN VARCHAR2 AS res varchar2(255);
will get a more elegant solution based on this.
Thanks
Thanks for appreciation :) keep visiting for more useful articles.
DeleteHow can we get the second last position , putting negative number for the gives an error. Please help.
ReplyDeleteselect * from
Delete(
SELECT REGEXP_SUBSTR(MYCOL, '[^,]+', 1, level) AS VAL, length(mycol) - length(replace(mycol,',')) + 2 - rownum rn
FROM
(
SELECT 'CAT,DOG,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS MYCOL FROM DUAL
)
connect by level <= length(mycol) - length(replace(mycol,','))+1
) where rn=2
also
Deleteconnect by level <= length(mycol) - length(replace(mycol,','))+1
can be replaced with
connect by REGEXP_SUBSTR(MYCOL, '[^,]+', 1, level) is not null
select substr(x,instr(x,',',-1,2)+1,instr(x,',',-1,1)-instr(x,',',-1,2)-1)
Deletefrom (select '45,44,58,99,99,100,200' x from dual)
Hi Nimish,
ReplyDeleteCan u suggest that how it is work for nth position.
Please explain it
REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 4)
Delete[^,]+ Says: Find until comma (,, any number of characters
1: start from 1st character
4: occurrence
Very much helpful
ReplyDeleteappreciate if you put demo videos of examples.
ReplyDeleteselect regexp_substr(data,'([a-zA-Z]{1,})',1,level) from ( SELECT 'CAT,DOG,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS data FROM DUAL ) connect by level <=regexp_count(data,',')+1 ;
ReplyDeleteREGEXP_SUBSTR(DATA,'([A-ZA-Z]{1,})',1,LEVEL)
--------------------------------------------------------------------------------
CAT
DOG
MOUSE
RAT
BAT
CAR
DOOR
LOCK
select regexp_substr(data,'([a-zA-Z]{1,})',1,level) from ( SELECT 'CAT,DOG,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS data FROM DUAL ) connect by level <=regexp_count(data,',')+1 ;
ReplyDeleteREGEXP_SUBSTR(DATA,'([A-ZA-Z]{1,})',1,LEVEL)
--------------------------------------------------------------------------------
CAT
DOG
MOUSE
RAT
BAT
CAR
DOOR
LOCK
how to get string between from_index and to_index specified ? like index 0 to 3rd
ReplyDeleteinput data = 'fsfa:c19a:2:66:0:0:0:1e01
output data = fsfa:c19a:2:66
Perfect, saved my day!
ReplyDeletehow to sum a delimited column in oracle?
ReplyDeleteSELECT REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 2) AS VAL
ReplyDeleteFROM
(
SELECT 'CAT,,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS MYCOL FROM DUAL
);
Not returns expected value if we have blank in 2nd position SELECT REGEXP_SUBSTR(MYCOL, '[^,]+', 1, 2) AS VAL
ReplyDeleteFROM
(
SELECT 'CAT,,MOUSE,RAT,BAT,CAR,DOOR,LOCK' AS MYCOL FROM DUAL
);