使用datetime值在ms access查询中筛选日期和时间

oyt4ldly  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(497)

例如,我想检索数据库中从前天晚上10点到昨天晚上10点之间的所有内容。我现在有这个格式(7/2/2020下午2:00)。

brc7rcf0

brc7rcf01#

Assuming table1 as table and col1 as column

declare @yesterday datetime
SELECT @yesterday=DATEADD(millisecond,DATEDIFF(millisecond, 0, (SELECT 
                  CONVERT(TIME,'22:00:00.000')) ),CAST( (convert(date, 
                  dateadd(day,datediff(day,1, GETDATE()),0)))  AS DATETIME));

declare @daybeforeyesterday datetime
SELECT @daybeforeyesterday=DATEADD(millisecond
                           ,DATEDIFF(millisecond, 0, (SELECT CONVERT(TIME,'22:00:00.000')) 
                           ),CAST( (convert(date, dateadd(day,datediff(day,2, 
                           GETDATE()),0)))  AS DATETIME));

select * from table1 where col1 between @yesterday and @daybeforeyesterday
vfh0ocws

vfh0ocws2#

只需使用 Date() (从午夜开始,'00:00:00')和 CDate (请注意,对于相同的值,时间格式可能不同,例如。, 10 PM22:00 ).

SELECT ...
FROM myTable
WHERE myDate BETWEEN (Date() - 2) + CDate('10 PM')
                 AND (Date() - 1) + CDate('22:00')
pgx2nnw8

pgx2nnw83#

计算小时数差异并使用日期添加:

Select *
From YourTable
Where YourDateField Between
    DateAdd("h", -26, Date()) And DateAdd("h", -2, Date())

相关问题