Home » SQL & PL/SQL » SQL & PL/SQL » How to discard the last character of a string
How to discard the last character of a string [message #507373] Sat, 14 May 2011 11:52 Go to next message
shabir46
Messages: 41
Registered: November 2009
Member
Hi ,

Please can someone tell me how we can discard the last character of a string in sql?

eg: we have a string v_str := 'abcde/xyz/eerr/';

I need to discard the last character '/' from this variable.

Is there any function available to do that?

Thanks n Regards,
Shabir
Re: How to discard the last character of a string [message #507376 is a reply to message #507373] Sat, 14 May 2011 12:05 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Is there any function available to do that?
SUBSTR() & LENGTH() functions

It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
Re: How to discard the last character of a string [message #507377 is a reply to message #507376] Sat, 14 May 2011 12:08 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
If the character to be trimmed off the end is always the same character, then you can use rtrim, as shown below.

SCOTT@orcl_11gR2> select rtrim ('abcde/xyz/eerr/', '/') from dual
  2  /

RTRIM('ABCDE/X
--------------
abcde/xyz/eerr

1 row selected.


If the last character could be any character, then you can use substr and length to select the characters from the first character to the length minus one character, as shown below.

SCOTT@orcl_11gR2> select substr ('abcde/xyz/eerr/', 1, length ('abcde/xyz/eerr/') - 1) from dual
  2  /

SUBSTR('ABCDE/
--------------
abcde/xyz/eerr

1 row selected.

SCOTT@orcl_11gR2>

Re: How to discard the last character of a string [message #507383 is a reply to message #507377] Sat, 14 May 2011 13:30 Go to previous message
Michel Cadot
Messages: 68763
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You can also use the regexp function:
SQL> select regexp_replace('abcde/xyz/eerr/','.$') from dual;
REGEXP_REPLACE
--------------
abcde/xyz/eerr

Regards
Michel
Previous Topic: need urgent help in using FORALL for inserting data(with some constant value) into table
Next Topic: Problem using select on Dba_pending_transactions inside PL SQL block
Goto Forum:
  


Current Time: Tue Jul 29 15:58:25 CDT 2025