How to validate zipcode using regular expression?
It should be in the following pattern:-
[A-z][0-9][A-Z] [0-9][A-Z][0-9]
E.g.
B5D 2M4
b5d 2m4
I am using Oracle9i.
Thanks in advance, Shubhojwal Ghosh
Unfortunately, since you are using a very old version of Oracle, you cannot use the standard regular expression functions like REGEXP_LIKE
. If you were to upgrade to a moderately recent version of Oracle, that would be the way to go.
In older versions of Oracle, you would have to use the OWA_PATTERN
package. Note that since the OWA_PATTERN.MATCH
function returns a BOOLEAN
, you cannot call it from SQL, just from PL/SQL. You could, of course, create your own wrapper function that returns an integer or a string to indicate whether the ZIP code is valid.
SQL> ed
Wrote file afiedt.buf
1 declare
2 function is_valid_zip( p_zip_code in varchar2 )
3 return boolean
4 is
5 begin
6 return owa_pattern.match( p_zip_code,
7 '[A-Z]{1}\d{1}[A-Z]{1}\d{1}[A-Z]{1}\d{1}',
8 'i' );
9 end is_valid_zip;
10 begin
11 if( is_valid_zip( 'A1B2C3' ) )
12 then
13 p.l( '1) Match' );
14 else
15 p.l( '1) No match' );
16 end if;
17 if( is_valid_zip( '12345' ) )
18 then
19 p.l( '2) Match' );
20 else
21 p.l( '2) No match' );
22 end if;
23* end;
SQL> /
1) Match
2) No match
PL/SQL procedure successfully completed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With