我正在尝试计算/选择一段时间内的数据字段。使用以下格式时,我明确指定整个时间戳,查询是正确的。
SELECT TimeStamp,strftime('%Y',TimeStamp) as "Year",
strftime('%m',TimeStamp) as "Month",
strftime('%d',TimeStamp) as "Day",
strftime('%M',TimeStamp) as "Minute",
strftime('%H', TimeStamp) as "Hours"
FROM temp
WHERE TimeStamp between '2017-12-22T20:50:00' and '2017-12-23T21:00:00'
我尝试做的是在一定时间内选择带有时间戳的字段。所以我将我的查询修改为:
SELECT TimeStamp,strftime('%Y',TimeStamp) as "Year",
strftime('%m',TimeStamp) as "Month",
strftime('%d',TimeStamp) as "Day",
strftime('%M',TimeStamp) as "Minute",
strftime('%H', TimeStamp) as "Hours"
FROM temp
WHERE strftime('%H', TimeStamp) between 20 and 21
然后查询返回0行,我在哪里做错了? 我的时间戳格式遵循sqlite约定为YYYY-mm-dd HH:MM:SS.3u。 以下是我的时间戳示例:2017-12-22T20:57:02.188Z
答案 0 :(得分:3)
SELECT TimeStamp,strftime('%Y',TimeStamp) as "Year",
strftime('%m',TimeStamp) as "Month",
strftime('%d',TimeStamp) as "Day",
strftime('%M',TimeStamp) as "Minute",
strftime('%H', TimeStamp) as "Hours"
FROM temp
WHERE cast(strftime('%H', TimeStamp) as int) between 20 and 21
strftime返回一个字符串,与int进行比较。将字符串转换为int以解决该问题
答案 1 :(得分:1)
您的指令strftime('%H', TimeStamp)
被视为字符串。因此无法与整数进行比较。您需要将其明确地转换为int。
CAST(strftime('%H', TimeStamp) AS INT)