Home » SQL & PL/SQL » SQL & PL/SQL » String Split/ String replace based on character length
String Split/ String replace based on character length [message #587886] Wed, 19 June 2013 03:38 Go to next message
nikhil9773
Messages: 1
Registered: June 2013
Junior Member
Here my problem in a notes column having 2000 characters max, i want my string output based on 35 characters, ya i need to replace
tag after 30 characters in the string.. example string "hi hello how are you doing out there, need your help!", i need out put as "hi hello how are you doing out
there, need your help! similar i need to calculate the sting length and have to split it 35+35+35.. i don't know how to perform this in sql/plsql.

this i tried

select substr(note,1,(instr(note, ' ',35)))||'
'||substr(note,instr(note, ' ',35),(instr(note, ' ',35)))notes from test
Re: String Split/ String replace based on character length [message #587888 is a reply to message #587886] Wed, 19 June 2013 03:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Welcome to the forum.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Before, Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.

Regards
Michel
Re: String Split/ String replace based on character length [message #587892 is a reply to message #587888] Wed, 19 June 2013 04:20 Go to previous messageGo to next message
Littlefoot
Messages: 21825
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
A simple solution might look like this; note that it doesn't take care about beauty (hint: hyphenation).
SQL> with test as
  2    (select 'hi hello how are you doing out there, need your help!' col from dual)
  3  select substr(col, 35 * (level - 1) + 1, 35) result
  4  from test
  5  connect by level <= length(col) / 35 + 1;

RESULT
-----------------------------------
hi hello how are you doing out ther
e, need your help!

SQL>
Re: String Split/ String replace based on character length [message #587895 is a reply to message #587892] Wed, 19 June 2013 04:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
And a simpler "solution" is also:
SQL> col col format a35
SQL> select 'hi hello how are you doing out there, need your help!' col from dual;
COL
-----------------------------------
hi hello how are you doing out ther
e, need your help!

Grin

Regards
Michel
Re: String Split/ String replace based on character length [message #587898 is a reply to message #587895] Wed, 19 June 2013 04:54 Go to previous messageGo to next message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

What Michel suggested only we can see that nowhere we can use.So question is Why to use sql commands then and where?

DECLARE
l_str VARCHAR2(200):='hi hello how are you doing out there, need your help!';
l_len integer:=35;
BEGIN
while LENGTH(l_str)<>0 loop 
dbms_output.put_line(substr(l_str,1,l_len));
l_str:=substr(l_str,l_len+1);
end loop;
end;
Re: String Split/ String replace based on character length [message #587900 is a reply to message #587886] Wed, 19 June 2013 04:56 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Hello,

You can use a row generator

COLUMN token FORMAT a50;
COLUMN token_length FORMAT 99;
SET LINESIZE 500;
SET PAGESIZE 200;

WITH tmptab AS
(
    -- length = 45
    SELECT '111111111111111111111111111111111111111111111' AS colval 
    FROM dual UNION ALL
    -- length = 5
    SELECT '22222' AS colval 
    FROM dual UNION ALL
    -- length = 35
    SELECT '33333333333333333333333333333333333' AS colval 
    FROM dual UNION ALL
    -- length = 34
    SELECT '4444444444444444444444444444444444' AS colval 
    FROM dual UNION ALL
    -- length = 36
    SELECT '555555555555555555555555555555555555' AS colval 
    FROM dual UNION ALL
    -- length = 71
    SELECT '66666666666666666666666666666666666666666666666666666666666666666666666' AS colval 
    FROM dual UNION ALL
    -- length = 80
    SELECT '77777777777777777777777777777777777777777777777777777777777777777777777777777777' AS colval 
    FROM dual
)
SELECT  substr
        (
            t1.colval, 
            ((t2.column_value - 1) * 35) + 1, 
            35
        ) token,
        length
        (
            substr
            (
                t1.colval, 
                ((t2.column_value - 1) * 35) + 1, 
                35
            )
        ) token_length,
        length(t1.colval) colval_length
FROM    tmptab t1 CROSS JOIN 
        TABLE
        (
            CAST
            (
                MULTISET
                (
                    SELECT level
                    FROM dual
                    CONNECT BY level <= ceil(length(t1.colval) / 35)
                ) AS sys.odciNumberList
            )
        ) t2;




TOKEN						   TOKEN_LENGTH COLVAL_LENGTH
-------------------------------------------------- ------------ -------------
11111111111111111111111111111111111			     35 	   45
1111111111						     10 	   45
22222							      5 	    5
33333333333333333333333333333333333			     35 	   35
4444444444444444444444444444444444			     34 	   34
55555555555555555555555555555555555			     35 	   36
5							      1 	   36
66666666666666666666666666666666666			     35 	   71
66666666666666666666666666666666666			     35 	   71
6							      1 	   71
77777777777777777777777777777777777			     35 	   80
77777777777777777777777777777777777			     35 	   80
7777777777						     10 	   80

13 rows selected.

SQL> 



Regards,
Dariyoosh

[Updated on: Wed, 19 June 2013 05:51]

Report message to a moderator

Re: String Split/ String replace based on character length [message #587929 is a reply to message #587886] Wed, 19 June 2013 06:47 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@nikhil9773,

Let's make it more generic using PL/SQL function:-
SQL> CREATE OR REPLACE TYPE strtype AS TABLE OF VARCHAR2 (255);
  2  /
 
Type created
 
SQL> 
SQL> CREATE OR REPLACE FUNCTION STR2SPLIT
  2         (P_STR       IN VARCHAR2,
  3          P_DELIM_LEN IN NUMBER)
  4  RETURN STRTYPE
  5  AS
  6     I_STR VARCHAR2(255);
  7     I_DELIM_LEN NUMBER;
  8     I_DATA STRTYPE := STRTYPE();
  9  BEGIN
 10     I_STR := P_STR;
 11     I_DELIM_LEN := P_DELIM_LEN;
 12     WHILE LENGTH(I_STR) <> 0 LOOP
 13        I_DATA.EXTEND;
 14        I_DATA(I_DATA.COUNT) := SUBSTR(I_STR, 1, I_DELIM_LEN);
 15        I_STR := SUBSTR(I_STR, I_DELIM_LEN + 1);
 16  
 17     END LOOP;
 18  
 19     RETURN I_DATA;
 20  END;
 21  /
 
Function created
 
SQL> SELECT *
  2    FROM TABLE(CAST(STR2SPLIT('hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!',
  3                              30) AS STRTYPE));
 
COLUMN_VALUE
--------------------------------------------------------------------------------
hi hello how are you doing out
 there, need your help!hi hell
o how are you doing out there,
 need your help!hi hello how a
re you doing out there, need y
our help!hi hello how are you
doing out there, need your hel
p!
 
8 rows selected
Re: String Split/ String replace based on character length [message #587955 is a reply to message #587929] Wed, 19 June 2013 08:31 Go to previous message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Lalit Kumar B wrote on Wed, 19 June 2013 07:47
Let's make it more generic using PL/SQL function:-


Don't use PL/SQL where you can do it with SQL. All that code is nothing but:

with t as (
           select  'hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!hi hello how are you doing out there, need your help!' str,
             30 chunk_size
             from  dual
          )
select  substr(str,chunk_size * (chunk_num - 1) + 1,chunk_size) chunk
  from  t,
        xmltable(
                 '1 to xs:integer(.)'
                 passing ceil(length(str) / chunk_size)
                 columns
                   chunk_num number path '.'
                )
  order by chunk_num
/

CHUNK
------------------------------
hi hello how are you doing out
 there, need your help!hi hell
o how are you doing out there,
 need your help!hi hello how a
re you doing out there, need y
our help!hi hello how are you
doing out there, need your hel
p!

8 rows selected.

SQL>  


SY.
Previous Topic: Diff between Alter Session and Alter system
Next Topic: date format
Goto Forum:
  


Current Time: Mon Aug 04 10:54:24 CDT 2025