<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>12Robots.com - Jason Dean - Oracle</title>
			<link>http://www.12robots.com/index.cfm</link>
			<description>ColdFusion, Database, MVC, Frameworks, Security and whatever</description>
			<language>en-us</language>
			<pubDate>Mon, 06 Sep 2010 19:11:45 -0500</pubDate>
			<lastBuildDate>Mon, 07 Apr 2008 08:14:52 -0500</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>jason@12robots.com</managingEditor>
			<webMaster>jason@12robots.com</webMaster>
			
			
			
			
			
			<item>
				<title>Oracle IsDate() Function</title>
				<link>http://www.12robots.com/index.cfm/2008/4/7/Oracle-IsDate()-Function</link>
				<description>
				
				&lt;p&gt;
	As promised in a previous &lt;a href=&quot;index.cfm?event=showEntry&amp;entryId=158ED9AD-19B9-BA51-EE6A527FD60C5F5F&quot;&gt;post&lt;/a&gt; here is my IsDate() function for Oracle 
&lt;/p&gt;

&lt;p&gt;
	I don&apos;t know how great a function it is, since I am a novice to Oracle, but it seems to work pretty well and very quickly.  I learned a lot while writing it, especially about basic excpetion handling in PL/SQL. 
	I&apos;m really not a big fan of Oracle, or PL/SQL, but I need to know it for my work, so I hope that my struggles in things like this can help others fuddle through.
&lt;/p&gt;

&lt;p&gt;Normally, I would use ColdFusions IsDate() function and be VERY happy with it. But in this case Iam importing batches of 1,000,000 records from raw data files and running conversion scripts 
on them, and ColdFusion just isn&apos;t built for that kind of work, so I am doing it directly in Oracle with PL/SQL and then calling that from ColdFusion. So far so good. It is a long road, since Iam new to Oracle there 
is a large learning curve for me. &lt;/p&gt;


&lt;p&gt;This Oracle function is pretty basic. It takes in a varchar2 and returns a varchar2(I would return a Boolean, but for whatever reason, in Oracle you don&apos;t get Boolean values for your $65,000 investment, and I cannot convert 1 and 0 to boolean. Stoopid Oracle). The it is the string that we would like to test.&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
		create or replace
			function IsDate (str varchar2) return varchar2 is
			inDate varchar2(40);
			
	&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
	Next we add a sub-function called dateCheck(). This takes in two parameters and returns one, all are varchar2 (see rant above). The first IN value is the string we passed into the 
	parent function witht he string we want to test. The second IN parameter is the mask of the date format we want to check the string against. The return string will be either &apos;true&apos; or &apos;false&apos;.
&lt;/p&gt;

&lt;p&gt;
	&lt;code&gt;
		 FUNCTION dateCheck (
			    inputDate varchar2, inputMask varchar2)
			    RETURN varchar2
			    IS
			    dateVar date;
			    BEGIN
			      dateVar:= to_date(inputDate,inputMask);
			      return &apos;true&apos;;
			      
			      exception
			        when others then
			        return &apos;false&apos;;
			  END;
	&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
	This sub-function uses the to_date() funciton in Oracle. to_date() tries to read in a string and convert it to a date using a user specified input mark. For example, to_date(06-18-2008, &apos;mm-dd-yyyy&apos;) would return a valid date, but to_date(06-18-2008, &apos;mm.dd.yyyy&apos;) would raise an exception.
	So since Oracle cannot return troe or false, I have create my function to retrun &apos;true&apos; if the command executes successfully and &apos;false&apos; if an exception is raised. 
&lt;/p&gt;	
			 
&lt;p&gt;
	Finally, my parent function will call the child function several times using different date pattern masks to see if any of them will work. If none of them evalute to true, the overall function will reutn &apos;false&apos;. If one of them wrks, then it is a valid date
	and the function will return &apos;true&apos;.
&lt;/p&gt;			   

&lt;p&gt;
	&lt;code&gt;
			  inDate:= trim(str);
			  if dateCheck(inDate, &apos;mm-dd-yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;mm-dd-yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy-mm-dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy-mm-dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy-mon-dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yy-mon-dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;dd-mon-yyyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;dd-mon-yy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mmddyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mmddyyyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yyyymmdd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yymmdd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yymmdd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yymondd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yyyymondd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm/dd/yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy/mm/dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm/dd/yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy/mm/dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm.dd.yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;mm.dd.yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy.mm.dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy.mm.dd&apos;) = &apos;false&apos; 
			  then
			   return &apos;false&apos;;
			  else
			    return &apos;true&apos;;
			  end if;
	&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
	So here is the overall code:
&lt;/p&gt;

&lt;p&gt;
	&lt;code&gt;
		create or replace
			function IsDate (str varchar2) return varchar2 is
			inDate varchar2(40);
			
			  FUNCTION dateCheck (
			    inputDate varchar2, inputMask varchar2)
			    RETURN varchar2
			    IS
			    dateVar date;
			    BEGIN
			      dateVar:= to_date(inputDate,inputMask);
			      return &apos;true&apos;;
			      
			      exception
			        when others then
			        return &apos;false&apos;;
			  END;
			   
			BEGIN
			  inDate:= trim(str);
			  if dateCheck(inDate, &apos;mm-dd-yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;mm-dd-yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy-mm-dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy-mm-dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy-mon-dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yy-mon-dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;dd-mon-yyyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;dd-mon-yy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mmddyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mmddyyyy&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yyyymmdd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yymmdd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yymmdd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yymondd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;yyyymondd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm/dd/yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy/mm/dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm/dd/yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy/mm/dd&apos;) = &apos;false&apos;
			    AND dateCheck(inDate, &apos;mm.dd.yyyy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;mm.dd.yy&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yyyy.mm.dd&apos;) = &apos;false&apos; 
			    AND dateCheck(inDate, &apos;yy.mm.dd&apos;) = &apos;false&apos; 
			  then
			   return &apos;false&apos;;
			  else
			    return &apos;true&apos;;
			  end if;
			  
			  --exception
			    --when others then return &apos;false&apos;;
			END; 
	&lt;/code&gt;
&lt;/p&gt;
			
			 
				
				</description>
						
				
				<category>Oracle</category>				
				
				<category>Database</category>				
				
				<pubDate>Mon, 07 Apr 2008 08:14:52 -0500</pubDate>
				<guid>http://www.12robots.com/index.cfm/2008/4/7/Oracle-IsDate()-Function</guid>
				
			</item>
			
		 	
			
			
			<item>
				<title>Oracle IsNumeric Function</title>
				<link>http://www.12robots.com/index.cfm/2008/4/3/Oracle-IsNumeric-Function</link>
				<description>
				
				&lt;p&gt;So, most people that use &lt;a href=&quot;http://www.oracle.com&quot;&gt;Oracle &lt;/a&gt;and other RDBMS know that they severly lack some very basic functions that exist in the middle-tier langauges we usually use. But sometimes we can;t use our middle-tier languages.&amp;nbsp; Right now I am writing a Stored Procedure in Oracle that will import, validate and normalize 1,000,000 records from raw text files.&amp;nbsp; This is WAY TOO big a job for &lt;a href=&quot;http://www.adobe.com/products/coldfusion/&quot;&gt;ColdFusion&lt;/a&gt;, so I am using Oracle Stored Procudres and functions.&lt;/p&gt;
&lt;p&gt;During my error checking I realized I needed some extra functionality. I needed IsDate() and IsNumeric(). With some Googling and simple Oracle functions, I was able to create both. today I will demostrate IsNumeric(). I am still working on adding more functionality to IsDate() and I will demo that one another day.&lt;/p&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red; font-weight: bold&quot;&gt;
UPDATE: I have updated this post to reflect a better way of handling an IsNumeric() function. This is from Alexandre Paiva&apos;s comment, thank you Alexandre!
&lt;/span&gt;
&lt;br /&gt;
&lt;code&gt;
	CREATE OR REPLACE FUNCTION isnumeric (p_string_value IN VARCHAR2)RETURN BOOLEAN
	AS

	test_value NUMERIC;
	BEGIN
		BEGIN
			test_value := TO_NUMBER (p_string_value);
			RETURN (true);

			EXCEPTION
			WHEN OTHERS THEN
			RETURN (false);
		END;
	END isnumeric;
&lt;/code&gt;
&lt;p&gt;This very simple function takes in a string of characters and passes it in. Then using the Oracle &lt;a href=&quot;http://www.techonthenet.com/oracle/functions/translate.php&quot;&gt;TRANSLATE &lt;/a&gt;function, it replaces any characters that are 1-9 or a decimal, plus sign or minus sign with a space (&amp;quot; &apos;). Then it does a trim on the remaining string, if the value is all spaces, the Length() function that wraps the others will return 0 and we know that the string that was passed in is numeric. If it returns 1 or more then we know there were non-numerics and we can return false.&lt;/p&gt;
				
				</description>
						
				
				<category>Oracle</category>				
				
				<category>Database</category>				
				
				<pubDate>Thu, 03 Apr 2008 13:31:00 -0500</pubDate>
				<guid>http://www.12robots.com/index.cfm/2008/4/3/Oracle-IsNumeric-Function</guid>
				
			</item>
			
		 	
			</channel></rss>