0

I am trying to automate API load and facing field restriction. Basically I need to shorten string if its more than 24 characters.

I am considering logic to take the first 4 characters of the word and replace space with _. A number of words in the field are dynamic.

e.g. Corporate Responsibility = copr_resp

E.g. Social Distancing Criteria = soci_dist_crit

Table_a

ColA Corporate Responsibility Social Distancing Criteria

Expected Result:

Select ColA, as Output from table_a;

Output ColA Output Corporate Responsibility copr_resp Social Distancing Criteria soci_dist_crit

I prefer to do this using SQL. Any suggestion?

2
  • what is the max length of the expected output strings if you have a field restriction? Commented Jun 26, 2020 at 15:07
  • 24 characters max. Commented Jun 26, 2020 at 16:18

1 Answer 1

2

You can divide the string into row and concat again as follows:

Select t.your_col,
       listagg( substr(regexp_substr(t.your_col,
                                    '[^ ]+', 1, levels.column_value), 
                       1, 4), 
              '_') within group (order by levels.column_value) as short_str
from your_table t
Cross join table(cast(multiset(select level from dual 
                 connect by level <= length (regexp_replace(t.your_col, '[^ ]+'))  + 1) 
                  as sys.OdciNumberList)) levels
Group by t.your_col
Sign up to request clarification or add additional context in comments.

2 Comments

almost there. I know I didnt mention it in original question. But is it possible to remove special character like & and - completely? Following gives output of : Lice_&_Lega_Resp, it will be nice to have lice_lega_resp
Select t.sub_division, listagg( substr(regexp_substr(t.sub_division, '[^ ]+', 1, levels.column_value), 1, 4), '_') within group (order by levels.column_value) as short_str from (SELECT 'Licensing & Legal Responsibility' sub_division FROM dual) t Cross join table(cast(multiset(select level from dual connect by level <= length (regexp_replace(t.sub_division, '[^ ]+')) + 1) as sys.OdciNumberList)) levels Group by t.sub_division;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.