SQL Developer中的设置时间范围

问题描述 投票:0回答:3

我正在处理一个包含车祸及其发生时间的数据集。 (数据集存在于SQL Server中,名称为accident)。

我有一列为date格式的列。我想从专栏中提取时间。然后添加一个新列lightining_period,将时间标记为daytimenighttime。我的问题是设置时间范围,因为每次运行代码时都会得到错误的标签。

不同的照明时段(白天:6AM-5:59 PM和夜间6PM-5:59 AM)。>>

[[1]首先,我编写了这段代码以从accident_date_time中提取时间并将其存储在新列time中。

create table lightiningPeriod as 
select to_char(accident_date_time,'HH:MMAM') as time
from accident.accident;

[[2]然后,我更改了表以在要在白天/夜晚存储标签的地方添加lightining_label列。

alter table lightiningPeriod add (
lightining_label varchar2(20)
);

[3]最后,我使用Update语句根据lightining_label范围更改time的值。但是桌子出来错了。我尝试使用between

to_datecastconvert,但是它们都不起作用。
update lightiningPeriod
set lightining_label='daytime'
where time >= '06:00AM'
and time <= '5:59PM';

下面是我得到的输出示例,显示了错误的标签。

time    lightining_label
06:04AM daytime
11:04AM daytime
01:04AM (null)
10:04AM daytime
10:04AM daytime
04:04PM (null)
07:04PM daytime
01:04PM (null)

我正在处理一个包含车祸及其发生时间的数据集。 (数据集以意外的名称存在于SQL Server中)。我有一个日期格式的列。我想要...

sql sql-server-2008 time range where-clause
3个回答
1
投票

似乎time列正在使用字符串类型(VARCHARNVARCHAR)而不是TIME。因此,您比较字符串值而不是时间值。


1
投票

这是塞巴斯蒂安的答案的调整。在SQL Server中,可以使用计算列:


0
投票

谢谢大家,我已经解决了此问题:

© www.soinside.com 2019 - 2024. All rights reserved.