我有数千条包含时间列的记录(行)。
我可以通过这样做轻松地在凌晨4点到凌晨5点之间获取时间行:
SELECT * FROM MyTable
WHERE CAST(Time AS TIME) BETWEEN '04:00' and '05:00'
要在下午6点到凌晨4点之间获取行:
SELECT * FROM MyTable
WHERE CAST(Time AS TIME) BETWEEN '18:00' and '04:00'
给我零行......
我该怎么做?
答案 0 :(得分:1)
将时间投入到某个时间是很奇怪的,但我认为它实际上是一个时间戳: - )
您也可以使用EXTRACT:
SELECT *
FROM MyTable
WHERE EXTRACT(HOUR FROM Time) >= 18
AND EXTRACT(HOUR FROM Time) < 4
答案 1 :(得分:0)
尝试:
SELECT *
FROM MyTable
WHERE CAST(Time AS TIME) NOT BETWEEN '04:00' AND '18:00'
between
(和not between
)的值必须按顺序排列,首先是较小的值。否则,什么都不匹配。
表达式:
x between a and b
完全等同于
((x> = a)和(x <= b))