Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateTime SQL

Author  Topic 

GradPS
Starting Member

9 Posts

Posted - 2012-01-13 : 07:53:36
Hi there, I am having problems inputting the datetime value into my database.

CAST('JAN-01-1990'as datetime)


gives me the following error:

quote:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


And that is the format I get the date from my front end so I really don't want to change it, is their anyway of getting it to work with SQL?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-13 : 08:12:39
Use convert like this:
CONVERT(DATETIME,REPLACE('JAN-01-1990','-',' '),100)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-01-13 : 08:15:06
SELECT CONVERT(DATETIME, REPLACE('JAN-01-1990', '-', ' '), 107)


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

GradPS
Starting Member

9 Posts

Posted - 2012-01-13 : 08:20:01
Thanks for the replies!

Can yous explain what that is doing, if you have some spare time, thank you.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-13 : 08:24:19
Both Peso's and mine do two things:

1. Replace the dashes with spaces.
2. Use the CONVERT function with a specific style to achieve the conversion. See here for details of various styles: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Go to Top of Page

GradPS
Starting Member

9 Posts

Posted - 2012-01-13 : 08:52:16
Thank you both.
Go to Top of Page
   

- Advertisement -