我必须找出表中的时间戳是上午、下午还是晚上。目前,我有以下代码:
case
when datepart(hour, o.timestamp) between 5 and 12
then 'Morning'
when datepart(hour, o.timestamp) between 13 and 17
then 'Afternoon'
when datepart(hour, o.timestamp) > 17
then 'Evening'
end
上面代码的问题是,2018-08-03 17:30:00.000是下午而不是晚上。这是因为2018-08-03 17:30:00.000中的小时是17,所以它被评估为下午。相反,我想包括分钟部分,然后它将成为晚上。
发布于 2019-03-01 05:49:16
时间比较怎么样?
(case when convert(time, o.timestamp) >= '05:00:00' and
convert(time, o.timestamp) < '12:00:00'
then 'morning'
when convert(time, o.timestamp) >= '12:00:00' and
convert(time, o.timestamp) < '17:00:00'
then 'afternoon'
else 'evening'
end)
请注意,您的逻辑没有考虑午夜到凌晨5点之间的时间。
你可以用小时本身做一些类似的事情,但我认为时间对你来说不会那么混乱。另外,我不知道早上的界限是中午还是下午1点。您的查询建议边界是下午1:00。常识告诉我们应该是中午。这在case
表达式中很容易调整。
https://stackoverflow.com/questions/54934771
复制相似问题