查询以在一天的两个时间之间进行选择 [英] Query to Select Between Two Times of Day

查看:19
本文介绍了查询以在一天的两个时间之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个查询,该查询将根据 DateTimeSyncDate 进行选择.

I am trying to build a query that will select based on a DateTime column SyncDate.

作为一些背景,我查询的表每天在完全相同的时间每天都有数千个导入.我想找到没有在一天中的常规"时间导入的条目.

As some background, the table I query from gets imports daily in the thousands at the exact same time everyday. I want to find the entries that did not import at the "regular" time of day.

因此,我想查询 DateTime 列中的时间在两次之间的位置:假设在任何日/月/年期间的 14:00-14:30 (2-230).

Therefore, I want to query where the time in the DateTime column is between two times: lets say 14:00-14:30 (2-230) on ANY day/month/year period.

SELECT * FROM MyTable
WHERE DatePart(SyncDate, ..?) BETWEEN (14:00..?) and (14:30..?)

DatePart 函数似乎是我需要的,但我不明白如何将其应用于这种情况.非常感谢您的帮助,伟大的查询者.

The DatePart function seems to be what I need, but I don't understand how to apply it to this situation. Your help is much appreciated oh great queriers.

我错了,我运行 SQL-Server-2005 作为我的后端.对不起!

I was mistaken, I am running SQL-Server-2005 as my backend. Sorry!

推荐答案

由于您使用的是 SQL Server 2008,您可以使用新的 TIME 数据类型:

Since you're on SQL Server 2008, you can use the new TIME datatype:

SELECT * FROM MyTable
WHERE CAST(SyncDate AS TIME) BETWEEN '14:00' and '14:30'

如果您的后端还不是 2008 年 :-) 那么您需要类似的东西:

If your backend isn't 2008 yet :-) then you'd need something like:

SELECT * FROM MyTable
WHERE DATEPART(HOUR, SyncDate) = 14 AND DATEPART(MINUTE, SyncDate) BETWEEN 0 AND 30

检查 14:00-14:30 小时.

to check for 14:00-14:30 hours.

这篇关于查询以在一天的两个时间之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆