Thursday, September 30, 2010

PL/SQL: How to Remove trailing String in Select Statement

Suppose you have a column where some of the values have a particular training string. and you want to remove this particular pattern in selected values.

You can use the below code to remove this type of string.

declare
Input varchar2(20) := 'my name :Keyword';
myResult varchar2(20) := '';
begin

select case when Input like '%:Keyword'  then 
substr(trim(Input),0,length(trim(Input))-8)
else
Input
end into myResult from dual;

dbms_output.put_line(myResult);
end;

This is general solution and you can use this solution to format your selected values.

You can use this solution in following cases while using the the table column:

1. Leading values formatting

2. trailing values formatting

3. any pattern formatting

0 comments:

Post a Comment

Related SQL Articles



Website List