简体   繁体   中英

round GETDATE (SQL Server)

I have a function which is working fine in MySQL

round((now()-ts/60) as tdiff

(round the result of subtracting the current datetime from ts (also a datetime) divided by 60)

Attempting (and failing) to convert this for 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 -

round((GETDATE()-ts/60,0) as tdiff

but that results in the datatypes (GETDATE and ts) are incompatible in the subtract operator.

So then I attempted to cast both GETDATE and ts as date and that made no difference.

ts is a conventional datetime ie 2918-04-20 11:05:09 and I assumed GETDATE returned the same format.

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.

Is there a remedy for this?

Regards, Ralph

GETDATE() , as per the documentation , returns a datetime . A datetime is accurate to 1/300 of a second, and it's accuracy cannot be changed.

If you want the time accurate to a second, you need to convert to a datetime2(0) :

SELECT CONVERT(datetime2(0),GETDATE());

Also, however, don't use syntax like GETDATE()-ts . use the functions DATEADD and DATEDIFF for date maths.

I've no idea what GETDATE()-ts/60 is trying to acheive. Perhaps the number of minutes between the 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. This is because the minute value has "ticked" once (from 0 to 1). Therefore you might want to use 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 ).

I think you want to use the DATEDIFF function:

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() . 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.

sorry, I don't know if I have understand the question, you need to do date-date/60 and round the result?

In this case you have to change the minus ("-") with the DATEDIFF("Type return example DAYS", GETDATE(), ts).

So you will have ROUND((DATEDIFF(DAY,GETDATE(),ts)/60,0)

Please try and let me know if it will works for you

Bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM