Date difference

1 Answer 281 Views
Forum suggestions
Kratik
Top achievements
Rank 1
Kratik asked on 28 Sep 2022, 07:24 AM

How can I get the number of seconds between NOW() and also a date in the database? I am following this article here to get a better idea about the concept of the date difference in SQL but am unable to understand it in a good way can someone here please guide me?

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Wissam
Telerik team
answered on 29 Sep 2022, 07:39 AM

Hello, Kratik,

The DATEDIFF takes 3 parameters:

- first, the `datepart` which is the part of the date you want as a result. In your case, it should be `second`

- the next two parameters are the `startdate` and `enddate`. The start date should be the date from your database, and `enddate` should be the function GETDATE.

The query will calculate the difference between both dates, and return the chosen `datepart` from it.

Here is what your query should look like:

SELECT DATEDIFF ( second , 'date from your database' , GETDATE() )

 

Note that, GETDATE was used instead of NOW. The Now function is specific to MYSQL, and getting the date difference is different in MYSQL, where TIMESTAMPDIFF is used instead of DATEDIFF.

For more information, please refer to these articles:

https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver16

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff

 

Regards,
Wissam
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Forum suggestions
Asked by
Kratik
Top achievements
Rank 1
Answers by
Wissam
Telerik team
Share this question
or