Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL declare datetime - 1001-01-01

I am trying to declare a datetime variable with the value 1001-01-01 00:00:00.000

I have tried the following approaches with no luck

declare @d1 datetime = '1001-01-01';
declare @d2 datetime = 10010101;
declare @d3 datetime = '1001-01-01 00:00:00';
declare @d4 datetime = cast ('1001-01-01' as datetime)

I get the following errors

Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type datetime.
Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Is it possible to declare a datetime variable which can hold the value 1001-01-01 00:00:00.000?

like image 901
dopplesoldner Avatar asked Mar 20 '13 15:03

dopplesoldner


People also ask

Can I use the date 9999-12-31 in SQL Server?

I recommend using it. This is not possible, because this is not a valid date in sql server. Sql server consider dates between 1753-01-01 and 9999-12-31. Check this link for more information: "DATETIME"

Is it possible to insert a 1753-01-01 date in SQL Server?

This is not possible, because this is not a valid date in sql server. Sql server consider dates between 1753-01-01 and 9999-12-31. Check this link for more information: "DATETIME" insert into my table (address, name, dateadded) values (?, ?, CASE WHEN ISDATE (?)=1 THEN ?

What is the earliest date a datetime value can have?

The earliest date that a datetime value can have is 1753-01-01. Recent versions of Sql Server have the data type datetime2 that has a larger range: Show activity on this post. The minimum SQL datetime is January 1,1753, so your date is indeed out-of-range.

What is the minimum SQL datetime range for a date?

The minimum SQL datetime is January 1,1753, so your date is indeed out-of-range. Show activity on this post. you will not be able to set a DateTime to that year, so you may want to store it as a string and leave it to the upper layer langague to deal with.


1 Answers

The minimum valid date for a DateTime data type is January 1, 1753.

Try DATETIME2, using:

DECLARE @d4 DATETIME2 = '1001-01-01'

The minimum valid date for DATETIME2 is 0001-01-01

like image 135
mattytommo Avatar answered Nov 23 '22 18:11

mattytommo