May 12th, 2006, 03:35 PM
Contributing User
Join Date: Sep 2005
Location: Hawaii
Finally found someone who needed same thing, give him credit for it.
SQL Code:
Original
- SQL Code
CREATE OR REPLACE FUNCTION getClassB ( ip IN VARCHAR2 DEFAULT NULL ) RETURN varchar2
/*
Author:Jonathon Wright
Version: (Release) 1.0.0
Notes: Takes in a VARCHAR IP, returns class B String
Would be same as "anding" the ip with 255.255.0.0
LEAVE THIS IN HEADER. FOR QUESTIONS: jonathon.wright@us.army.mil
*/
IS
posA NUMBER; --position in string to end of first octet
posB NUMBER; --position in string to end of second octet
first_octet VARCHAR2( 3 ) ;
second_octet VARCHAR2( 3 ) ;
classB VARCHAR2( 20 ) ;
BEGIN
--get position to first period
SELECT instr( ip,'.' ) INTO posA FROM dual;
--get first octet
SELECT substr( ip, 1 , posA - 1 ) INTO first_octet FROM dual;
--get position to 2nd period
SELECT instr( substr( ip, posA + 1 ) ,'.' ) INTO posB FROM dual;
--get 2nd octet
SELECT substr( ip,posA + 1 , posB - 1 ) INTO second_octet FROM dual;
--return classB
classB := first_octet || '.' || second_octet || '.0.0' ;
RETURN classB;
exception WHEN others THEN
dbms_output.put_line( 'SQLERR:' || SQLCODE || '-' || SQLERRM) ;
END getClassB;
/
Comments on this post
pabloj
agrees: Thank you for posting the solution