简单的SQL Server SELECT不使用日期

时间:2015-12-15 21:18:11

标签: datetime select sql-server-2012

我有一个简单的表格,其中有3列与我关注

  • TargetUnits(真实)
  • TargetDate(datetime)
  • ProductID(int)

当我运行以下SELECT时,我会在1015-12-10那天获得一些日期时间。这很好用,我得到了结果

SELECT 
    [TargetDate], [TargetUnits] 
FROM 
    [TaktBoards].[dbo].[TargetPlot] 
WHERE 
    [ProductID] = 2 
    AND [TargetDate] > '2015-12-10 00:00:00'  
ORDER BY 
    [TargetDate] DESC

输出:

TargetDate  TargetUnits
--------------------------------
2015-12-10 23:45:00.000 50
2015-12-10 23:30:00.000 49.15254
2015-12-10 23:15:00.000 48.30508
2015-12-10 23:00:00.000 47.45763
2015-12-10 22:45:00.000 46.61017
2015-12-10 22:30:00.000 45.76271
2015-12-10 22:15:00.000 44.91525
2015-12-10 22:00:00.000 44.91525
2015-12-10 21:45:00.000 44.06779
2015-12-10 21:30:00.000 43.22034
2015-12-10 21:15:00.000 42.37288
2015-12-10 21:00:00.000 41.52542
2015-12-10 20:45:00.000 40.67797
2015-12-10 20:30:00.000 39.83051
2015-12-10 20:15:00.000 38.98305
2015-12-10 20:00:00.000 38.98305
2015-12-10 19:45:00.000 38.13559
2015-12-10 19:30:00.000 37.28814
2015-12-10 19:15:00.000 36.44068
2015-12-10 19:00:00.000 35.59322
2015-12-10 18:45:00.000 34.74576
2015-12-10 18:30:00.000 33.8983
2015-12-10 18:15:00.000 33.05085
2015-12-10 18:00:00.000 32.20339
2015-12-10 17:45:00.000 32.20339
2015-12-10 17:30:00.000 32.20339
2015-12-10 17:15:00.000 31.35593
2015-12-10 17:00:00.000 30.50847
2015-12-10 16:45:00.000 30.50847
2015-12-10 16:30:00.000 29.66102
2015-12-10 16:15:00.000 28.81356
2015-12-10 16:00:00.000 27.9661
2015-12-10 15:45:00.000 27.11864
2015-12-10 15:30:00.000 26.27119
2015-12-10 15:15:00.000 25.42373
2015-12-10 15:00:00.000 24.57627
2015-12-10 14:45:00.000 23.72881
2015-12-10 14:30:00.000 22.88136
2015-12-10 14:15:00.000 22.0339
2015-12-10 14:00:00.000 22.0339
2015-12-10 13:45:00.000 21.18644
2015-12-10 13:30:00.000 20.33898
2015-12-10 13:15:00.000 19.49153
2015-12-10 13:00:00.000 18.64407
2015-12-10 12:45:00.000 17.79661
2015-12-10 12:30:00.000 16.94915
2015-12-10 12:15:00.000 16.94915
2015-12-10 12:00:00.000 16.94915
2015-12-10 11:45:00.000 16.10169
2015-12-10 11:30:00.000 15.25424
2015-12-10 11:15:00.000 14.40678
2015-12-10 11:00:00.000 13.55932
2015-12-10 10:45:00.000 12.71186
2015-12-10 10:30:00.000 11.86441
2015-12-10 10:15:00.000 11.01695
2015-12-10 10:00:00.000 10.16949
2015-12-10 09:45:00.000 9.322034
2015-12-10 09:30:00.000 8.474576
2015-12-10 09:15:00.000 7.627119
2015-12-10 09:00:00.000 7.627119
2015-12-10 08:45:00.000 7.627119
2015-12-10 08:30:00.000 6.779661
2015-12-10 08:15:00.000 5.932203
2015-12-10 08:00:00.000 5.084746
2015-12-10 07:45:00.000 4.237288
2015-12-10 07:30:00.000 3.389831
2015-12-10 07:15:00.000 2.542373
2015-12-10 07:00:00.000 1.694915
2015-12-10 06:45:00.000 0.8474576
2015-12-10 06:30:00.000 0

但是,当我运行此SELECT时(仅添加[TargetDate] < '2015-12-10 03:56:00'),我没有得到任何结果。

SELECT 
    [TargetDate], [TargetUnits] 
FROM 
    [TaktBoards].[dbo].[TargetPlot] 
WHERE 
    [ProductID] = 2 
    AND [TargetDate] > '2015-12-10 00:00:00' 
    AND [TargetDate] < '2015-12-10 03:56:00' 
ORDER BY 
    [TargetDate] DESC

我做错了什么?我希望获得12月10日下午不到3:56的所有记录。请帮忙。 :)

安迪

3 个答案:

答案 0 :(得分:2)

[TargetDate] < '2015-12-10 03:56:00

当你的最小记录是 2015-12-10 06:30:00.000 0
哪个比你的竞争对手大。也许你的意思是

[TargetDate] > '2015-12-10 03:56:00

答案 1 :(得分:0)

你似乎想要这个:

TargetDate < '2015-12-10 15:56:00' -- 24-hour clock

另一种选择是使用允许您指定am/pm的文字格式。

https://msdn.microsoft.com/en-us/library/ms187819.aspx

答案 2 :(得分:0)

下午3点应该表示为15:00。您的数据(如果是全部数据)在0到03:00范围内没有任何内容