I have a secret to forcing data. I see two different behaviors for the same query, and I cannot understand why.
This is an excerpt from the corresponding part of the request in question with fixed values. The first value represents βtodayβ in our query and is configured with the same data type with an explicit CAST:
SELECT DATEDIFF(dd,CAST('2014-03-24' AS SmallDateTime),'0001-01-01')
ISO 8601 Date in literal format: https://msdn.microsoft.com/en-us/library/ms187819.aspx
We have two different databases on the same instance of the SQL server.
One of them returns zero rows, as you would expect.
The server returns an error about the date range '1/1/0001':
Msg 242, Level 16, State 3, Line 1
Converting the varchar data type to the datetime data type resulted in a value out of range.
Two servers return a value that looks correct:
-735315
The problematic date is almost certainly "1/1/0001", and it fails because I expect the default datetime to be lower than the minimum SQL data time of January 1, 1753 ( https://msdn.microsoft.com /en-us/library/ms187819.aspx ).
According to the MSDN page for dated ( https://msdn.microsoft.com/en-US/library/ms189794(v=SQL.105).aspx ), it can take the following values:
startdate is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset
The translation results for each of them are identical between the servers and are listed here:
SELECT CAST('0001-01-01' As time)
SELECT CAST('0001-01-01' As date)
SELECT CAST('0001-01-01' As smalldatetime)
SELECT CAST('0001-01-01' As DateTime)
SELECT CAST('0001-01-01' As datetime2)
SELECT CAST('0001-01-01' As datetimeoffset)
DateTime, .
, , , .
SQL, .
, , , , ( SQL Server Management Studio):
( , ):
(database) > Right Click > Properties > General > Maintenance > Collation
Database one: SQL_Latin1_General_CP1_CI_AS
Database two: SQL_Latin1_General_CP1_CI_AS
:
(database) > Right Click > Properties > Options > Misc. > Date Correlation Optimization Enabled
Database one: False
Database two: False
:
(database) > Right Click > Properties > Options > Containment > Two Digit Year Cutoff
Database one: 2049
Database two: 2049
DBCC USEROPTIONS
Database one, dateformat: mdy
Database two, dateformat: mdy
(other settings appear identical)
, , .
- ? , ?
:
- ISO, - . .
- ββ DBCC USEROPTOINS dateformat, mdy