Seohan
Seohan

Reputation: 65

Delete Last Character

StringX Varchar(30);

StringX is not a column in a table .

I have StringX = 'He is Teacher , '

How I can delete the last char ( , )?

Upvotes: 7

Views: 13876

Answers (4)

ilhan kaya
ilhan kaya

Reputation: 51

Hi,

There are you can try this code block; if your string is the last character is ',' (NULL character is ignored) then this code scope delete it.

select case
     when substr(replace(StringX
                        ,' ')
                ,length(replace(StringX
                               ,' '))
                ,1) = ',' then

      substr(StringX
            ,1
            ,instr(StringX
                  ,','
                  ,-1) - 1)

     else
      null
   end
from dual

Upvotes: 0

Jad Chahine
Jad Chahine

Reputation: 7149

You can use this :

Select StringX as BeforeRemovalOfLastCharacter, left (StringX , len (StringX )-1) as AfterRemoveLastCharacter

Upvotes: 1

Nikhil Batra
Nikhil Batra

Reputation: 3148

You can use SUBSTR() as:

select substr(stringX,1,length(stringX)-1)

The SUBSTR function return a portion of char, beginning at character position, substring_length characters long.

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 156978

You can use rtrim for that:

select rtrim(StringX, ', ')
from   tabkeName

Also note you have a space before and after the ,, so you need both , and in the rtrim call.

Upvotes: 7

Related Questions