Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

error converting string to datetime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
stormogulen - 09 Jul 2008 09:05 GMT
Hi All!

I'm having some problems converting a string to a datetime.

For some reason some dates in my db are stored in varchar-fields. I
have to convert these to datetimes, because I have to make
calculations with them.

This seems to work well and automatic on SQL Server 2005.

But in some countries I get reports back, about errors that occur when
this is done on SQL server 2000.
I know that I can use the CONVERT function, but I cannot seem to
figure out which format the old data that is already in the db have,
and therefore which format code I should use.

Does anybody know what affects the format that SQL server
automatically choses for date formats? I would think that I could
change the regional settings of my local machine, and recreate the
error, but I cannot.

Best regards
Bjarne
Uri Dimant - 09 Jul 2008 09:16 GMT
Hi
http://www.karaszi.com/SQLServer/info_datetime.asp

> Hi All!
>
[quoted text clipped - 19 lines]
> Best regards
> Bjarne
Aaron Bertrand [SQL Server MVP] - 09 Jul 2008 13:51 GMT
Since you are storing the datetime values in varchar format, it is virtually
impossible to tell what the date values really should be.  Imagine you have:

05/06/07 04:32
2008-04-01 06:45
20040431 04:45
06/05/07 04:55

Now you can easily weed out row 3 because it is not a date.  But for rows 1
and 4, which one is May 6th 2007, which is June 5th 2007, are are they both
the same?  Who knows?

I don't want to sound like Celko, but fix the design.  Store datetime values
in datetime columns, and then your problem at least partially disappears.
(You still need to ensure that you use parameterized statements when saving
data, and that your application correctly interprets string literals entered
by users.)

If you need to continue storing them in varchar columns, then fix the
application.  Make sure string literals are stored in an unambiguous format
that is not vulnerable to regional settings, locale, dateformat settings,
language, etc.  For dates only:

YYYYMMDD

For dates with time:

YYYY-MM-DDTHH:MM:SS.mmm

Yes, that T is meant to be there.

On 7/9/08 4:05 AM, in article
f0aa47dc-9aca-4863-b4af-cd49ee1b9714@34g2000hsh.googlegroups.com,

> Hi All!
>
[quoted text clipped - 19 lines]
> Best regards
> Bjarne
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.