C P
C P

Reputation: 31

SQL Server date format handling

I am running the SAME Delphi 2007 code on two different Windows 7 machines and accessing the SAME SQL server database on a remote server. On one machine a date in the database is read in the format 2013-01-25 00:00:00.000 which causes the application to crash when it tries to convert this date to a DateTime. On the other machine a date is read in the format 1/25/2013 which is correctly converted to a DateTime without any issues. What is causing this difference? (I verified that the Control Panel 'Region and Language' settings on the two machines are identical). The dates in the database are stored as datetime2.

Upvotes: 1

Views: 1522

Answers (1)

Kaf
Kaf

Reputation: 33809

On one machine a date in the database is read in the format 2013-01-25 00:00:00.000 which causes the application to crash when it tries to convert this date to a DateTime. On the other machine a date is read in the format 1/25/2013 which is correctly converted to a DateTime without any issues

I don't know anything about Delphi but on the SQL Server side:

Date is a Date and does not have a format. If your column is of Date datatype, you shouldn't get any errors when converting to DateTime.

I suspect you may be storing date in a string type field in which case it would be best to use culture in-specific ISO or ISO8601 format before converting to DateTime.

Upvotes: 2

Related Questions