T-SQL Between Dates Confusion

后端 未结 5 489
一生所求
一生所求 2020-12-03 18:22

I am working with T-SQL in SQL Server 2000 and I have a table TRANSACTIONS which has a date column TRANDATE defined as DateTime, among many other c

5条回答
  •  长情又很酷
    2020-12-03 19:16

    You have kind of answered your own question already. What you have observed is the way SQL Server works.

    If it is confirmation you need, this MSDN document has following to say about it

    When the time part is unspecified, it defaults to 12:00 A.M. Note that a row that contains a time part that is after 12:00 A.M. on 1998-0105 would not be returned by this query because it falls outside the range.

    Edit

    As for your comment, a datetime essentially is a floating point value.

    Following script shows what numbers SQL Server works with.
    40541.9749 (12/31/2010 23:23:59) can't be included when your upper bound is 40541 (12/31/2010)

    DECLARE @ADateTime1 DATETIME
    DECLARE @ADateTime2 DATETIME
    DECLARE @ADateTime1AsFloat FLOAT
    DECLARE @ADateTime2AsFloat FLOAT
    
    SET @ADateTime1 = '12/31/2010'
    SET @ADateTime2 = '12/31/2010 23:23:59'
    
    SET @ADateTime1AsFloat = CAST(@ADateTime1 AS FLOAT)
    SET @ADateTime2AsFloat = CAST(@ADateTime2 AS FLOAT)
    
    SELECT @ADateTime1AsFloat, @ADateTime2AsFloat
    

提交回复
热议问题