在Sql Server查询当天数据或某天数据,传入指定某天数据值为条件,这里我们提供了两种方案:
第一种,采用“DATEADD”和“DATEDIFF”组合,结合“WHERE … BETWEEN … AND”查询;第二种,采用“WHERE … YEAR … MONTH … DAY”。
第一种查询:利用“DATEADD”取时间值,“DATEDIFF”设置开始时间
DECLARE @TodayStart datetime
DECLARE @TodayEnd datetime
SET @TodayStart = DATEADD(dd, DATEDIFF(dd, 0, @TodayStart datetime), 0) -- example: year-month-day 00:00:00.000
SET @TodayEnd = DATEADD(ss, -1, DATEADD(dd, 1, @TodayStart)) -- example: year-month-day 23:59:59.000
SELECT *FROM [Accesslog].[dbo].[Refresh_Data]
WHERE [Refresh_DateTime] BETWEEN @TodayStart AND @TodayEnd
第二种查询:利用“YEAR、MONTH、DAY”函数
SELECT * FROM Refresh_Data WHERE YEAR(Refresh_DateTime)=YEAR('" + time + "')
and MONTH(Refresh_DateTime)=MONTH('" + time + "') and DAY(Refresh_DateTime)=DAY('" + time + "')
ORDER BY Refresh_DateTime DESC