IsDate() and SQL Server

SQL Server, ColdFusion

I don't know about you, but my company uses the IsDate() function for a lot of simple validation of date inputs, and it works fairly well for the most part. The only problem I have with it is that when paired with SQL Server (2000 or 2005), it still can't capture all invalid dates.

The date 'January 5, 753' is a valid date, but attempting to insert it into a SQL Server datetime object throws an error because valid dates for SQL Server are between January 1, 1753 and December 31, 9999. Dates above the year 9999 are caught by IsDate(), but the lower bound is not.

<cffunction name="IsSQLServerDate" returntype="boolean" output="false">
   <cfargument name="date" type="date" required="true"/>
   <cfargument name="type" type="string" required="false" default="datetime"/>

   <cfswitch expression="#arguments.type#">
      <cfcase value="datetime">
         <cfreturn IsDate(arguments.date) AND Year(arguments.date) gte 1753 />
      </cfcase>
      <cfcase value="smalldatetime">
         <cfreturn IsDate(arguments.date) AND DateCompare(arguments.date, '1/1/1900') gte 0 AND DateCompare(arguments.date, '6/2/2079') lte 0 />
      </cfcase>
   </cfswitch>
</cffunction>

Nothing that someone else couldn't write, but useful. to save headaches.

Recursive CTE in SQL Server 2005

SQL Server

Recursive Common Table Expressions are solving a lot of problems for me at work today, so I thought I'd throw a note up on here about them. Its a common problem with databases that records often have a parent record in the same table, which can in turn have another parent and so forth up the chain. What happens is that at some point you need to find out the whole structure of the tree, which becomes much more difficult. Enter RCTE. While I've known that other servers had capabilities like this for a while (Oracle for example), I'd not seen a good way to do it in SQL Server.

Find out a little more at the bottom of this page.

And an official Microsoft page can be found here.

Testing YouTube Video content.

If it proves too annoying, I'll get rid of it.

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Archives By Subject

Ajax (1) [RSS]
BlogCFC (2) [RSS]
CF8 (8) [RSS]
cfimage (7) [RSS]
ColdFusion (15) [RSS]
css (5) [RSS]
frameworks (9) [RSS]
Funny (2) [RSS]
Google (1) [RSS]
IIS (1) [RSS]
Java in ColdFusion (4) [RSS]
javascript (7) [RSS]
Licensing (2) [RSS]
Model Glue (2) [RSS]
Projects (3) [RSS]
Prototype (2) [RSS]
Spry (1) [RSS]
SQL Server (2) [RSS]
User Interface Design (1) [RSS]
Web Services (1) [RSS]

RSS