best way to convert and validate a date string

First of all, since you’re using SQL Server 2005, you should put your code that might fail into BEGIN TRY.....END TRY BEGIN CATCH....END CATCH blocks – try/catch blocks for T-SQL!

Second, for all date manipulation, I would always use ISO-8601 format which will work regardless of what current date format is set in SQL Server.

ISO-8601 format is YYYYMMDD for just dates, or YYYY-MM-DDTHH:MM:SS for date with time – so I’d write your code as:

BEGIN TRY
  SET @Source="07152009"
  SET @Temp = RIGHT(@Source, 4) +             -- YYYY
              LEFT(@Source, 2) +              -- MM
              SUBSTRING(@Source, 3, 2)        -- DD

  IF ISDATE(@Temp)!=1
  BEGIN
      RAISERROR('ERROR, invalid date',16,1)
  END

  SET @Destination = CAST(@Temp AS DATETIME)
END TRY
BEGIN CATCH
      -- handle error if something bombs out
END CATCH

Do not rely on any particular date format being set!! Send me your code and I’ll try it on a Swiss-German system – I almost guarantee it’ll break if you blindly assume “en-US” and thus “mm/dd/yyyy” – it’s not the same setting everywhere on this planet.

Unfortunately SQL Server is rather weak handling dates – maybe that might be an extension point where using a CLR assembly inside SQL Server would make sense, to tap into the much richer date handling functions in .NET ??

Marc

PS: seems the ISO-8601 format I knew YYYY-MM-DD doesn’t always work in SQL Server – contrary to what Books Online seem to preach. Use YYYYMMDD or YYYY-MM-DDTHH:MM:SS instead.
Thanks, gbn!

Leave a Comment