Home » SQL & PL/SQL » SQL & PL/SQL » Replace Non Alpha Numeric Characters
Replace Non Alpha Numeric Characters [message #13242] Thu, 01 July 2004 10:10 Go to next message
DG
Messages: 5
Registered: October 2000
Junior Member
Hi All,

I have a function that will replace the contents of the input parameter and replace any non-numeric characters.  I just want to know if there is a more efficient way to code this (oracle 8i or higher).  Thanks in advanced for any input.

function strip_non_numeric(p_string in varchar2) return varchar2 is
Result varchar2(100) := '' ;
x_length number;
begin
SELECT LENGTH(p_string) INTO X_LENGTH FROM DUAL;
FOR i in 1..X_LENGTH LOOP
if (SUBSTRB(nvl(p_string,'0'),i,1)) in ('0','1','2','3','4','5','6','7','8','9') then
result := concat(result,(SUBSTRB(p_string,i,1)));
end if;
END LOOP;
return(Result);
exception when others then return ' ';
end strip_non_numeric;
Re: Replace Non Alpha Numeric Characters [message #13249 is a reply to message #13242] Thu, 01 July 2004 20:54 Go to previous messageGo to next message
S.Rajesh Kannan
Messages: 26
Registered: September 2003
Junior Member
Hi,

SQL> create table test_table(data varchar2(100));

Table created.

SQL> insert into test_table values('Test456ing');

1 row created.

SQL> insert into test_table values('55*^$asdf');

1 row created.

SQL> insert into test_table values('12355');

1 row created.

SQL> select * from test_table;

DATA
------------
Test456ing
55*^$asdf
12355

The following function would simply replace the non numeric characters;


SQL> create or replace function strip_non_numeric(p_string varchar2)
  2  return varchar2 is
  3  result varchar2(100):='';
  4  begin
  5     select translate(p_string,  6  '0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*~!#$%^()_-+=&#124}]{{/?.,><','0')
  7     into result from dual;
  8     return result;
  9  exception when others then return '';
 10* end strip_non_numeric;
SQL> /

Function created.

SQL> select strip_non_numeric(data) from test_table;

STRIP_NON_NUMERIC(DATA)
-----------------------------------------------------
456
55
12355


You can also use the following SQL Query directly,

SQL>select translate(data,'0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*~!#$%^()_-+=&#124}]{{/?.,><','0') "Result" from test_table;

Result
-------------------------------------------------
456
55
12355

-S.Rajesh Kannan

Re: Replace Non Alpha Numeric Characters [message #13257 is a reply to message #13249] Fri, 02 July 2004 04:40 Go to previous messageGo to next message
shoblock
Messages: 325
Registered: April 2004
Senior Member
well, 1st off, the longer the input string, the worse the loop method becomes. For short strings, looping or using translate are about the same performance, so I always use translate.

2nd, the above posted function is overly complex. There's no need to select from dual (decode is about the only thing that needs this, as it doesn't work in pl/sql, only sql). So, save some code, don't limit yourself to a 100 chars for return, and use this:

create or replace function strip_non_numeric(p_string varchar2) return varchar2
begin
return ( translate(p_string,
'0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*~!#$%^()_-+=&#124}]]{{/?.,><',
'0') );
end strip_non_numeric;

You may also want to add non-displayed chars to the above (depending on your input, they may or may not matter): '....,><' || chr(9) ||chr(10)...
Re: Replace Non Alpha Numeric Characters [message #13261 is a reply to message #13257] Fri, 02 July 2004 13:47 Go to previous messageGo to next message
DG
Messages: 5
Registered: October 2000
Junior Member
Thanks for the suggestions. I created the following function. For my application, I just want alpha numeric characters so I commented those characters sets out.

create or replace function
strip_non_alpha_numeric (p_string varchar2)
return varchar2
as
begin
return (translate(p_string,
chr(1) ||
chr(2) ||
chr(3) ||
chr(4) ||
chr(5) ||
chr(6) ||
chr(7) ||
chr(8) ||
chr(9) ||
chr(10) ||
chr(11) ||
chr(12) ||
chr(13) ||
chr(14) ||
chr(15) ||
chr(16) ||
chr(17) ||
chr(18) ||
chr(19) ||
chr(20) ||
chr(21) ||
chr(22) ||
chr(23) ||
chr(24) ||
chr(25) ||
chr(26) ||
chr(27) ||
chr(28) ||
chr(29) ||
chr(30) ||
chr(31) ||
chr(32) ||
chr(33) ||
chr(34) ||
chr(35) ||
chr(36) ||
chr(37) ||
chr(38) ||
chr(39) ||
chr(40) ||
chr(41) ||
chr(42) ||
chr(43) ||
chr(44) ||
chr(45) ||
chr(46) ||
chr(47) ||
/* chr(48) ||
chr(49) ||
chr(50) ||
chr(51) ||
chr(52) ||
chr(53) ||
chr(54) ||
chr(55) ||
chr(56) ||
chr(57) ||
*/ chr(58) ||
chr(59) ||
chr(60) ||
chr(61) ||
chr(62) ||
chr(63) ||
chr(64) ||
/* chr(65) ||
chr(66) ||
chr(67) ||
chr(68) ||
chr(69) ||
chr(70) ||
chr(71) ||
chr(72) ||
chr(73) ||
chr(74) ||
chr(75) ||
chr(76) ||
chr(77) ||
chr(78) ||
chr(79) ||
chr(80) ||
chr(81) ||
chr(82) ||
chr(83) ||
chr(84) ||
chr(85) ||
chr(86) ||
chr(87) ||
chr(88) ||
chr(89) ||
chr(90) ||
*/ chr(91) ||
chr(92) ||
chr(93) ||
chr(94) ||
chr(95) ||
chr(96) ||
/* chr(97) ||
chr(98) ||
chr(99) ||
chr(100) ||
chr(101) ||
chr(102) ||
chr(103) ||
chr(104) ||
chr(105) ||
chr(106) ||
chr(107) ||
chr(108) ||
chr(109) ||
chr(110) ||
chr(111) ||
chr(112) ||
chr(113) ||
chr(114) ||
chr(115) ||
chr(116) ||
chr(117) ||
chr(118) ||
chr(119) ||
chr(120) ||
chr(121) ||
chr(122) ||
*/ chr(123) ||
chr(124) ||
chr(125) ||
chr(126) ||
chr(127) ||
chr(128) ||
chr(129) ||
chr(130) ||
chr(131) ||
chr(132) ||
chr(133) ||
chr(134) ||
chr(135) ||
chr(136) ||
chr(137) ||
chr(138) ||
chr(139) ||
chr(140) ||
chr(141) ||
chr(142) ||
chr(143) ||
chr(144) ||
chr(145) ||
chr(146) ||
chr(147) ||
chr(148) ||
chr(149) ||
chr(150) ||
chr(151) ||
chr(152) ||
chr(153) ||
chr(154) ||
chr(155) ||
chr(156) ||
chr(157) ||
chr(158) ||
chr(159) ||
chr(160) ||
chr(161) ||
chr(162) ||
chr(163) ||
chr(164) ||
chr(165) ||
chr(166) ||
chr(167) ||
chr(168) ||
chr(169) ||
chr(170) ||
chr(171) ||
chr(172) ||
chr(173) ||
chr(174) ||
chr(175) ||
chr(176) ||
chr(177) ||
chr(178) ||
chr(179) ||
chr(180) ||
chr(181) ||
chr(182) ||
chr(183) ||
chr(184) ||
chr(185) ||
chr(186) ||
chr(187) ||
chr(188) ||
chr(189) ||
chr(190) ||
chr(191) ||
chr(192) ||
chr(193) ||
chr(194) ||
chr(195) ||
chr(196) ||
chr(197) ||
chr(198) ||
chr(199) ||
chr(200) ||
chr(201) ||
chr(202) ||
chr(203) ||
chr(204) ||
chr(205) ||
chr(206) ||
chr(207) ||
chr(208) ||
chr(209) ||
chr(210) ||
chr(211) ||
chr(212) ||
chr(213) ||
chr(214) ||
chr(215) ||
chr(216) ||
chr(217) ||
chr(218) ||
chr(219) ||
chr(220) ||
chr(221) ||
chr(222) ||
chr(223) ||
chr(224) ||
chr(225) ||
chr(226) ||
chr(227) ||
chr(228) ||
chr(229) ||
chr(230) ||
chr(231) ||
chr(232) ||
chr(233) ||
chr(234) ||
chr(235) ||
chr(236) ||
chr(237) ||
chr(238) ||
chr(239) ||
chr(240) ||
chr(241) ||
chr(242) ||
chr(243) ||
chr(244) ||
chr(245) ||
chr(246) ||
chr(247) ||
chr(248) ||
chr(249) ||
chr(250) ||
chr(251) ||
chr(252) ||
chr(253) ||
chr(254) ||
chr(255),'0'));
end strip_non_alpha_numeric;
Re: Replace Non Alpha Numeric Characters [message #523858 is a reply to message #13261] Tue, 20 September 2011 14:04 Go to previous messageGo to next message
mhampton
Messages: 2
Registered: September 2011
Junior Member
With the introduction of REGEXP_REPLACE in 10g, stripping non-numeric characters from a string became a lot easier:

SELECT REGEXP_REPLACE('123AnythingElse','[^0-9]') FROM DUAL;
Re: Replace Non Alpha Numeric Characters [message #523865 is a reply to message #523858] Tue, 20 September 2011 14:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68763
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Who is interested in an answer to a 7 years old topic?
It is nice you answer but choose an active topic.
Also 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" button to verify.

Regards
Michel
Re: Replace Non Alpha Numeric Characters [message #523867 is a reply to message #523865] Tue, 20 September 2011 16:02 Go to previous message
mhampton
Messages: 2
Registered: September 2011
Junior Member
I'm well aware of how old it is, but believe it or not, if you google "pl/sql remove non-numeric characters" this is the very first thing to pop up. I thought I'd share a better solution.

Try it.
Previous Topic: Regexp
Next Topic: How to select Latest Updated record from table
Goto Forum:
  


Current Time: Wed Jul 23 23:09:22 CDT 2025