Dealing with Dates as Strings Part II (and Errors in SQL 2000)

by matt 20. April 2007 07:43
Heh, a slight addition to my previous post here.  Kinda feeling sheepish now.

My code worked frin for about 8 hours or su, until randomly, my client in question decided to switch date formats.  I can't really figure out why, but intermittantly, the thing calling my code will send 4/20/2007 or 20/4/2005.  This kinda sucks so I have to have another scurry around on google to see what I could find.  Basically, this article  by Amol Kulkarni here helped me out alot.

You have to be careful with XACT_ABORT I think, again - I'm not SQL guru and I'll need to read up on this.  It seems fairly similar to VB's 'On Error Resume Next' and as such could be quite dangerous if not handled correclty.

Either way, my lovely SQL code now caters for dd/mm/yyyy and mm/dd/yyyy looks like this:

Declare @DateTime varchar(50)
Declare @dtmDate datetime
Set @Datetime = '4/20/2007 12:03:56 AM'

Set XACT_ABORT OFF

Select @dtmDate = Convert(varchar, Convert(datetime, @DateTime, 103), 109)
If (@@Error <> 0) Select @dtmDate    = Convert(varchar, Convert(datetime, @DateTime, 101), 109)

Set XACT_ABORT ON

Print @dtmDate

Hopefully that's that end of it :)

Tags:

SQL Server

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar