简体   繁体   English

如何从SQL中获取当前日期仅两小时分钟的数据

[英]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.

相关问题 如何在距前一天仅两个小时的时间内获取数据, - How to get data for just two hour from previous day, SQL 获取一天中的每一小时和每一分钟 - SQL get every hour and minute of day Hive SQL 中的 second()、minute()、hour() 等是否有毫秒等价? 如果没有,我怎样才能从日期中提取毫秒? - Is there a milliseconds equivalent for second(), minute(), hour(), etc. in Hive SQL? If not, how can I extract just the milliseconds from a date? 如何使用datediff函数显示日,时,分和秒 - How to use datediff function to show day, hour, minute, and second 如何在SQL中合并两个DateTime部分(小时,分钟,秒)? - How Can I merge two DateTime parts (Hour, Minute, Second) in SQL? 通过SQL对星期几和小时/分钟进行双重分组 - Double group by SQL for day of week and hour/minute 如何在 SQL Server 中以小时、天、分钟、秒显示浮点数 - how to show float numbers in Hours, Day, Minute, Second in SQL Server 如何使用date_add将年,月,日,时,分,秒添加到mysql日期? - How can I add Year, Month, Day, Hour, Minute, Second to mysql date using date_add? HQL如何处理星期几和日期相等,而没有小时,分钟,秒? - HQL How to process equals for day of week and date without hour,minute,second? 如何从 SQL 服务器获取当前星期的数据从星期六开始在星期五结束 - How to get data of current week Start on Saturday end in Friday from SQL Server with current day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM