23

我有一个表,其中有一列“已创建”作为日期时间。

我正在尝试查询以检查 Created 值的时间是否在两次之间。

第一行的 Created datetime 是“2013-07-01 00:00:00.000”(午夜),我正在尝试查询时间在晚上 11 点到早上 7 点之间的项目。

select *
from MyTable
where CAST(Created as time) between '23:00:00' and '06:59:59'

但没有返回任何结果。

我需要将时间转换为日期时间吗?

4

8 回答 8

48

我怀疑你想检查它是晚上 11 点之后还是早上 7点之前:

select *
from MyTable
where CAST(Created as time) >= '23:00:00' 
   or CAST(Created as time) < '07:00:00'
于 2013-07-01T14:58:21.697 回答
12
select *
from MyTable
where CAST(Created as time) not between '07:00' and '22:59:59 997'
于 2013-07-01T15:17:47.933 回答
5

我有一个非常相似的问题,想分享我的解决方案

鉴于此表(所有 MySQL 5.6):

create table DailySchedule
(
  id         int auto_increment primary key,
  start_time time not null,
  stop_time  time not null
);

x (hh:mm:ss)选择给定时间介于开始时间和停止时间之间的所有行。包括第二天。

注意:替换为您喜欢NOW()的任何时间x

SELECT id
FROM DailySchedule
WHERE
  (start_time < stop_time AND NOW() BETWEEN start_time AND stop_time)
  OR
  (stop_time < start_time AND NOW() < start_time AND NOW() < stop_time)
  OR
  (stop_time < start_time AND NOW() > start_time)

结果

给定

  id: 1, start_time: 10:00:00, stop_time: 15:00:00
  id: 2, start_time: 22:00:00, stop_time: 12:00:00
  • 选定的行NOW = 09:00:002
  • 选定的行NOW = 14:00:001
  • 选定的行NOW = 11:00:001,2
  • 选定的行NOW = 20:00:00:无
于 2018-11-15T13:19:57.570 回答
1

这也应该有效(即使在 SQL-Server 2005 中):

SELECT *
FROM dbo.MyTable
WHERE Created >= DATEADD(hh,23,DATEADD(day, DATEDIFF(day, 0, Created - 1), 0))
  AND Created <  DATEADD(hh,7,DATEADD(day, DATEDIFF(day, 0, Created), 0))

演示

于 2013-07-01T15:05:14.050 回答
1
WITH CTE as
(
SELECT CAST(ShiftStart AS DATETIME) AS ShiftStart, 
CASE WHEN ShiftStart > ShiftEnd THEN CAST(ShiftEnd AS DATETIME) +1
ELSE CAST(ShiftEnd AS DATETIME) END AS ShiftEnd
FROM **TABLE_NAME**
)
SELECT * FROM CTE
WHERE 
CAST('11:00:00' AS DATETIME) BETWEEN ShiftStart AND ShiftEnd -- Start of Shift
OR CAST('23:00:00' AS DATETIME) BETWEEN ShiftStart AND ShiftEnd -- End of Shift
于 2020-01-23T07:36:16.030 回答
0

让我们考虑一个存储班次详细信息的表

在此处输入图像描述

请检查 SQL 查询以生成表并根据输入(时间)查找时间表

声明表变量

declare @MyShiftTable table(MyShift int,StartTime time,EndTime time)

将值添加到表变量

insert into @MyShiftTable select 1,'01:17:40.3530000','02:17:40.3530000'
insert into @MyShiftTable select 2,'09:17:40.3530000','03:17:40.3530000'
insert into @MyShiftTable select 3,'10:17:40.3530000','18:17:40.3530000'

使用名为“Flag”的附加字段创建另一个表变量

declare @Temp table(MyShift int,StartTime time,EndTime time,Flag int)

通过交换开始和结束时间向临时表添加值

插入@Temp 选择 MyShift,case when (StartTime>EndTime) then EndTime else StartTime end,case when (StartTime>EndTime) then StartTime else EndTime end,case when (StartTime>EndTime) then 1 else 0 end from @MyShiftTable

创建输入变量以查找 Shift

declare @time time=convert(time,'10:12:40.3530000')

查询以查找与提供的时间相对应的班次

从@Temp 中选择 myShift,其中
(@time 在 StartTime 和 EndTime 之间且 Flag=0)或(@time not 在 StartTime 和 EndTime 之间且 Flag=1)

于 2018-12-06T12:19:12.067 回答
0
select * from dbMaster oMaster  where ((CAST(GETDATE() as time)) between  (CAST(oMaster.DateFrom as time))  and  
(CAST(oMaster.DateTo as time)))

请检查这个

于 2020-12-17T17:45:34.373 回答
-2

应该是 AND 而不是 OR

select *
from MyTable
where CAST(Created as time) >= '23:00:00' 
   AND CAST(Created as time) < '07:00:00'
于 2016-10-03T22:27:33.067 回答