-2

I have a requirement to make data in oracle as below:

Condition :- Values till last comma (Maximum 20 Chars).

Data                       | Length | Output
----------------------------------------------------
12,34,abc,D,eva,096,qwerty | 26     | 12,34,abc,D,eva,096
----------
12,34,abc,DEF,0014,nish    | 23     | 12,34,abc,DEF,0014
----------
12,34,abc,DEF,0014,gup,    | 23     | 12,34,abc,DEF,0014
----------
12,34,abc,D,eva            | 15     | 12,34,abc,D,eva
----------

I Have tried below query to get data, its giving result with comma in last and i don't know how to restrict string limit in regexp.

SELECT regexp_substr('12,34,abc,DEF,0014,nish', '(.)+,') FROM dual;

2 Answers2

2

The following will give the result you seem to be looking for, but I'm not sure it's correct as I suspect there's more to the assignment than has been mentioned:

SELECT LINE,
       LENGTH(LINE) AS ORIGINAL_LENGTH,
       CASE
         WHEN LENGTH(LINE) > 20 THEN
           SUBSTR(LINE, 1, INSTR(TRIM(',' FROM LINE), ',', -1)-1)
         ELSE LINE
       END AS RESULT
  FROM DATA

SQLFiddle here

Best of luck.

0

Try regexp_replace(substr(your_string||',', 1, 21), ',[^,]*$')

Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23