[英]round GETDATE (SQL Server)
I have a function which is working fine in MySQL 我有一个在MySQL中正常工作的函数
round((now()-ts/60) as tdiff
(round the result of subtracting the current datetime from ts (also a datetime) divided by 60) (将ts(也是日期时间)减去当前日期时间后的结果四舍五入)
Attempting (and failing) to convert this for SQL Server. 尝试(失败)将其转换为SQL Server。
Tried - 尝试-
round((GETDATE()-ts/60) as tdiff
but that results in round function requires 2 or 3 parameters (which to me it does), so modified to - 但这导致回合函数需要2或3个参数(对我来说确实如此),因此将其修改为-
round((GETDATE()-ts/60,0) as tdiff
but that results in the datatypes (GETDATE and ts) are incompatible in the subtract operator. 但这导致数据类型(GETDATE和ts)在减法运算符中不兼容。
So then I attempted to cast both GETDATE and ts as date and that made no difference. 因此,我尝试将GETDATE和ts都强制转换为日期,这没有什么区别。
ts is a conventional datetime ie 2918-04-20 11:05:09 and I assumed GETDATE returned the same format. ts是传统的日期时间,即2918-04-20 11:05:09,我假设GETDATE返回了相同的格式。
As an example if GETDATE is today and ts is 2018-04-20 11:05:09 then tdiff is 6850891 (round effectively removes the dashes and colons and concatenates the datetime producing 20180420110509 for 2018-04-20 11:05:09 and 20180831164000 for 2018-08-31 16:40:00 and then divides by 60 to get 6850891. 例如,如果GETDATE是今天并且ts是2018-04-20 11:05:09,则tdiff是6850891(有效舍入去除破折号和冒号并将日期时间连接为2018-04-20 11:05:09的日期时间,并且20180831164000为2018-08-31 16:40:00然后除以60得到6850891。
Is there a remedy for this? 是否有补救措施?
Regards, Ralph 问候,拉尔夫
GETDATE()
, as per the documentation , returns a datetime
. 根据文档 , GETDATE()
返回datetime
。 A datetime
is accurate to 1/300 of a second, and it's accuracy cannot be changed. datetime
时间精确到1/300秒,并且其准确度不能更改。
If you want the time accurate to a second, you need to convert to a datetime2(0)
: 如果您希望时间精确到一秒,则需要将其转换为datetime2(0)
:
SELECT CONVERT(datetime2(0),GETDATE());
Also, however, don't use syntax like GETDATE()-ts
. 但是,也不要使用像GETDATE()-ts
这样的语法。 use the functions DATEADD
and DATEDIFF
for date maths. 对日期数学使用函数DATEADD
和DATEDIFF
。
I've no idea what GETDATE()-ts/60
is trying to acheive. 我不知道GETDATE()-ts/60
试图达到什么目的。 Perhaps the number of minutes between the 2? 也许2分钟之间的分钟数是多少? DATEDIFF
counts the "ticks" between 2 dates/times, thus DATEDIFF(MINUTE,'00:00:59','00:01:00')
would return 1
, despite there only being 1 second between the 2 times. DATEDIFF
计算2个日期/时间之间的“滴答声”,因此DATEDIFF(MINUTE,'00:00:59','00:01:00')
将返回1
,尽管两次之间只有1秒。 This is because the minute value has "ticked" once (from 0 to 1). 这是因为分钟值已经“滴答”了一次(从0到1)。 Therefore you might want to use DATEDIFF(SECOND,'00:00:59','00:01:00') / 60
. 因此,您可能要使用DATEDIFF(SECOND,'00:00:59','00:01:00') / 60
。 This would return 0, as 1 / 60
in integer math is 0 (as is 59 / 60
). 这将返回0,如1 / 60
在整数运算为0(这是59 / 60
)。
I think you want to use the DATEDIFF function: 我认为您想使用DATEDIFF函数:
DATEDIFF ( datepart , startdate , enddate )
DATEDIFF ( second, ts, GETDATE())
DATEDIFF ( second, ts, GETDATE())
DATEDIFF ( minute, ts, GETDATE())
DATEDIFF ( hour, ts, GETDATE())
The first argument tells it which increment of time to return. 第一个参数告诉它要返回的时间增量。
If you are trying to find the difference between two values, then use datediff()
. 如果要查找两个值之间的差异,请使用datediff()
。 For instance: 例如:
select datediff(day, getdate(), ts)
gets the difference in days. 得到天的差异。
date_diff()
or a related function would also be the right approach in MySQL. date_diff()
或相关函数在MySQL中也是正确的方法。
sorry, I don't know if I have understand the question, you need to do date-date/60 and round the result? 抱歉,我不知道我是否理解这个问题,您需要执行date-date / 60并取整结果吗?
In this case you have to change the minus ("-") with the DATEDIFF("Type return example DAYS", GETDATE(), ts). 在这种情况下,您必须使用DATEDIFF(“ Type return example DAYS”,GETDATE(),ts)更改减号(“-”)。
So you will have ROUND((DATEDIFF(DAY,GETDATE(),ts)/60,0) 因此您将拥有ROUND((DATEDIFF(DAY,GETDATE(),ts)/ 60,0)
Please try and let me know if it will works for you 请尝试让我知道它是否适合您
Bye 再见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.