If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > trim spaces between words of a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-04-03, 10:30
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
trim spaces between words of a string

I want to trim spaces between two or more words of a string by single space. for eg:- input string is 'HIGHLAND RD' and output string should be 'HIGHLAND RD'.
i was using REPLACE but it doesn' work as i thought first
v_streetNameAndType :=
REPLACE(LTRIM(p_streetNameAndType),' ',' ');

wondering is there any alternate way to do this.

i am newbie to oracle and haven't used any function.

if i need a function for above , is this close
======================================
FUNCTION SqueezeToOneSpace ( p_instr IN VARCHAR2,
p_outstr OUT VARCHAR2) RETURN VARCHAR2 IS


v_instr VARCHAR2(64);
v_tmpstr VARHCAR2(64);



BEGIN

v_instr:=p_instr;
FOR ' ' IN v_inStr LOOP

v_tmpstr=REPLACE(v_instr,' ',' ');

END LOOP;

p_outstr:=v_tmpstr;

END;
======================
Please help
Reply With Quote
  #2 (permalink)  
Old 09-04-03, 12:58
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: trim spaces between words of a string

Quote:
Originally posted by skd
I want to trim spaces between two or more words of a string by single space. for eg:- input string is 'HIGHLAND RD' and output string should be 'HIGHLAND RD'.
i was using REPLACE but it doesn' work as i thought first
v_streetNameAndType :=
REPLACE(LTRIM(p_streetNameAndType),' ',' ');

wondering is there any alternate way to do this.

i am newbie to oracle and haven't used any function.

if i need a function for above , is this close
======================================
FUNCTION SqueezeToOneSpace ( p_instr IN VARCHAR2,
p_outstr OUT VARCHAR2) RETURN VARCHAR2 IS


v_instr VARCHAR2(64);
v_tmpstr VARHCAR2(64);



BEGIN

v_instr:=p_instr;
FOR ' ' IN v_inStr LOOP

v_tmpstr=REPLACE(v_instr,' ',' ');

END LOOP;

p_outstr:=v_tmpstr;

END;
======================
Please help
Try this:
Code:
CREATE OR REPLACE
FUNCTION SqueezeToOneSpace
  ( p_instr IN VARCHAR2
  ) RETURN VARCHAR2
IS
  v_outstr VARCHAR2(32767) := p_instr;
BEGIN
  WHILE v_outstr LIKE '%  %' LOOP
    v_outstr := REPLACE( v_outstr, '  ', ' ' );
  END LOOP;
  RETURN v_outstr;
END;
/
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 09-04-03, 13:53
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Thumbs up Re: trim spaces between words of a string

Quote:
Originally posted by andrewst
Try this:
Code:
CREATE OR REPLACE
FUNCTION SqueezeToOneSpace
  ( p_instr IN VARCHAR2
  ) RETURN VARCHAR2
IS
  v_outstr VARCHAR2(32767) := p_instr;
BEGIN
  WHILE v_outstr LIKE '%  %' LOOP
    v_outstr := REPLACE( v_outstr, '  ', ' ' );
  END LOOP;
  RETURN v_outstr;
END;
/
Thanks for quick help. wondering if this can handle tab between words?
Reply With Quote
  #4 (permalink)  
Old 09-04-03, 13:59
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: trim spaces between words of a string

Quote:
Originally posted by skd
Thanks for quick help. wondering if this can handle tab between words?
Tab is CHR(9), so just TRANSLATE( string, CHR(9), ' ' ) to turn tabs to spaces before doing the rest.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 09-04-03, 14:11
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Cool Re: trim spaces between words of a string

Quote:
Originally posted by andrewst
Tab is CHR(9), so just TRANSLATE( string, CHR(9), ' ' ) to turn tabs to spaces before doing the rest.
THANKS VERY MUCH
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On