Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value

Using SQL Server 2008.I have a table called User which has a column LastLogindata with datetimeoffset datatype

The following query works on production server but not on replication server.

select top 10 CAST(LastLoginDate AS DATETIME)  from User.

I am getting the following error.The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value.

Thanks

like image 267
paraaku chiraaku Avatar asked Jul 11 '12 16:07

paraaku chiraaku


2 Answers

Check the LastLoginDate columns value like this '0001-01-01' or '0001/01/01'.

If u have means get this error ..

Try this one

select top 10  CAST(CASE when cast(LastLoginDate  as varchar) = '0001-01-01 00:00:00' 
                         THEN NULL ELSE GETDATE() end AS DATETIME) from User
like image 63
poongunran Avatar answered Oct 12 '22 11:10

poongunran


If a field in database is of type datetimeoffset type, then it should contain date within range 0001-01-01 through 9999-12-31. I think the issue is the date inside your database.

Please check the official link of SQL server Click Here

like image 25
Subhransu Sekhar Avatar answered Oct 12 '22 11:10

Subhransu Sekhar