Fedor Ivanov
Fedor Ivanov

Reputation: 281

How to get seconds between two dates in SQL Server?

I had this MySQL code

DELETE FROM UserError WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(date) > 86400

What is equivalent of this MySQL code in SQL Server?

Upvotes: 25

Views: 47168

Answers (4)

radar
radar

Reputation: 13425

You can use datediff

DELETE FROM UserError WHERE 
Datediff(s, [date], getdate()) > 86400

Upvotes: 48

Dhwani
Dhwani

Reputation: 7626

Try this:

SELECT DATEDIFF(second, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); 

--Syntax
       DATEDIFF ( datepart , startdate , enddate )

Upvotes: 5

Krish KvR
Krish KvR

Reputation: 1054

DATEDIFF ( ss , startdate , enddate )

Upvotes: 1

danish
danish

Reputation: 5600

You can make use of DateDiff for this. It looks like this:

DateDiff(datePart,startDate,endDate)

Upvotes: 3

Related Questions