Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Where date is greater than or equal to today in SQLite

I have dates stored in my Database in a dd/mm/yyyy format and I am looking to only show items where the date is equal to or great than today.

I have tried WHERE date(fixtures.date) >= date('now') but I got no results.

Below is my query, any help would be greatly appreciated

SELECT fixtures.id, 
       fixtures.team_1_id, 
       fixtures.team_2_id, 
       fixtures.date, 
       fixtures.time, 
       fixtures.pitch, 
       teams.team_name, 
       teams_1.team_name AS awayTeam, 
       leagues.league_name 
FROM   fixtures 
       INNER JOIN teams 
               ON fixtures.team_1_id = teams.id 
       INNER JOIN teams AS teams_1 
               ON fixtures.team_2_id = teams_1.id 
       INNER JOIN teams_vs_leagues 
               ON teams.id = teams_vs_leagues.team_id 
       INNER JOIN leagues 
               ON teams_vs_leagues.league_id = leagues.id 
WHERE  date(fixtures.date) >= date('now') 
ORDER  BY fixtures.date DESC 
like image 378
MMcK Avatar asked Apr 22 '16 03:04

MMcK


People also ask

How do I check if a date is greater than today 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.

How do I get today's date in SQLite?

The SQLite function DATE('now') returns the current date as a text value. It uses the 'YYYY-MM-DD' format, where YYYY is a 4-digit year, MM is a 2-digit month, and DD is a 2-digit day of the month. This function takes one argument: the text 'now' indicates the current date and time.

What is the use of date () function in SQLite?

The SQLite date() function is used to calculate the date and return it in the format 'YYYY-MM-DD'. The SQLite datetime() function is used to calculate a date/time value, and return it in the format 'YYYY-MM-DD HH:MM:SS'. The SQLite julianday() function returns the date according to julian day.

How do I create a date field in SQLite?

First, create a new table named datetime_real . Second, insert the “current” date and time value into the datetime_real table. We used the julianday() function to convert the current date and time to the Julian Day. Third, query data from the datetime_real table.


1 Answers

Why do you say your dates are store in dd/mm/yyyy format? According to SQLite docs

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  1. TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").

  2. REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.

  3. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

Basically this query won't work

select 1 where '01-08-2016' >= date('now')

but this will

select 1 where '2016-08-01'>= date('now')

So can you verify if your date fixtures.date is format as 'yyyy-MM-dd' otherwise your query won't work. Additionally remember to use date('now', 'localtime') to get your local time.

If your date is well formated you can try to do something like

SELECT fixtures.id
FROM   fixtures
WHERE  fixtures.date >= date('now')

if you do get results with this, then the joins are not matching any row.

For further information you can check this answer

like image 178
ingkevin Avatar answered Sep 23 '22 17:09

ingkevin