Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Query condition for future date time

Tags:

sql

sql-server

Im having trouble with this query. I want to not select records that have passed the SYSTIME for the current date but display records for future dates even if they have passed the current SYSTIME

SELECT * 
  FROM TABLE
 WHERE DATE>= CONVERT(date, SYSDATETIME())
   AND STARTTIME > CONVERT(time, SYSDATETIME())

This is the query. I know why it doesnt work but I can't think of a way to do what I stated above.

like image 512
user2321895 Avatar asked May 11 '13 20:05

user2321895


People also ask

How can I get future date in SQL?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.

How do I query a timestamp in SQL?

To get a day of week from a timestamp, use the DAYOFWEEK() function: -- returns 1-7 (integer), where 1 is Sunday and 7 is Saturday SELECT dayofweek('2018-12-12'); -- returns the string day name like Monday, Tuesday, etc SELECT dayname(now()); To convert a timestamp to a unix timestamp (integer seconds):

How do you check if one date is greater than another in SQL?

In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a 'YYYY-MM-DD hh:mm: ss. mmm' pattern.


1 Answers

SELECT * 
FROM TABLE
WHERE 
  (
   (DATE = CONVERT(date, SYSDATETIME() 
    AND STARTTIME > CONVERT(time, SYSDATETIME()
   )
    OR Date > sysdatetime()
  )

You need an or condition since date time are in different fields you must first resolve today's date and time and then all future dates regardless of time.

like image 168
xQbert Avatar answered Nov 15 '22 00:11

xQbert