[英]How to get data for just two hour minute second from current day in sql
Here I am trying to display hour, minute and second. 在这里,我试图显示小时,分钟和秒。 ie current time is 10 am in the morning and I need data of 9 am. 即当前时间是上午10点,我需要9点的数据。 so here it display only hour not the actual time. 因此这里只显示小时,而不显示实际时间。 Current time is 10.33 AM and I expect 9.33 AM data but the output shows only 9.00 AM data instead of current time. 当前时间是10.33 AM,我希望获得9.33 AM数据,但是输出仅显示9.00 AM数据,而不是当前时间。
set @EndHour = dateadd(hh,datediff(hh,0,GETDATE()),0)
set @StartHour = dateadd(hh,-1,@EndHour)
Current time is 10.33 AM and I expect 9.33 AM data but the data is displaying of 9.00 AM 当前时间是10.33 AM,我希望有9.33 AM数据,但是数据显示为9.00 AM
You definition of @EndHour
is truncating the minutes and seconds. 您对@EndHour
定义将截断分钟和秒。
If you want the value to the nearest minute, perhaps you want: 如果您希望该值最接近分钟,则可能需要:
set @EndHour = dateadd(minute, datediff(minute, 0, GETDATE()), 0)
set @StartHour = dateadd(hour, -1, @EndHour);
Or use timefromparts()
: 或者使用timefromparts()
:
select timefromparts(datepart(hour, getdate()) - 1,
datepart(minute, getdate()),
0, 0, 0
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.