Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare datetimes in SQL so that equal dates are excluded?

Tags:

sql

sql-server

I have a statement setup as follows:

SELECT *
FROM Events
WHERE CAST('6/6/2013 10:14:30 PM' AS DATETIME) < EventTime

Right now, it returns every event including those occurring at that time, which means I have to process the events after the query. Is there a way to setup this query so that it only returns events with an EventTime that is greater than the given string datetime?

Basically, the < operator seems to be treated as <=, and I don't want this.

like image 783
jtfairbank Avatar asked Jan 24 '26 02:01

jtfairbank


1 Answers

You could use DATEDIFF function of sql server, like such:

SELECT *
FROM Events
WHERE DATEDIFF(Second, Cast('6/6/2013 10:14:30 PM' AS DATETIME) , @EventTime) < 0 
             --^^ this can be changed to what ever accuracy you want
like image 132
Java Devil Avatar answered Jan 25 '26 22:01

Java Devil