SQL Select between dates

SqlSqlite

Sql Problem Overview


I am running sqlite to select data between two ranges for a sales report. To select the data from between two dates I use the following statement:

SELECT * FROM test WHERE date BETWEEN "11/1/2011" AND "11/8/2011";

This statement grabs all the dates even those outside the criteria. The date format you see entered is in the same format that I get back. I'm not sure what's wrong.

Sql Solutions


Solution 1 - Sql

SQLLite requires dates to be in YYYY-MM-DD format. Since the data in your database and the string in your query isn't in that format, it is probably treating your "dates" as strings.

Solution 2 - Sql

Change your data to that formats to use sqlite datetime formats.

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD

SELECT * FROM test WHERE date BETWEEN '2011-01-11' AND '2011-08-11'

Solution 3 - Sql

One more way to select between dates in SQLite is to use the powerful strftime function:

SELECT * FROM test WHERE strftime('%Y-%m-%d', date) BETWEEN "11-01-2011" AND "11-08-2011"

These are equivalent according to https://sqlite.org/lang_datefunc.html:

date(...)

strftime('%Y-%m-%d', ...)

but if you want more choice, you have it.

Solution 4 - Sql

SELECT *
FROM TableName
WHERE julianday(substr(date,7)||'-'||substr(date,4,2)||'-'||substr(date,1,2)) BETWEEN julianday('2011-01-11') AND julianday('2011-08-11')

Note that I use the format: dd/mm/yyyy. If you use d/m/yyyy, Change in substr().

Solution 5 - Sql

Or you can cast your string to Date format with date function. Even the date is stored as TEXT in the DB. Like this (the most workable variant):

SELECT * FROM test WHERE date(date) 
BETWEEN date('2011-01-11') AND date('2011-08-11')

Solution 6 - Sql

SQLite does not have a concept of dates. It only knows them as text. When you do this in SQLite you're actually doing string comparisons. You can read more from the official documentation.

> When two TEXT values are compared an appropriate collating sequence is used to determine the result.

Any numeric (i.e., not using words like 'May') format for dates that is padded and in order from biggest field to smallest field will work. "2021-05-07" (May 7th) comes before "2021-05-09" (May 9th). So if you use "yyyy-mm-dd" format then you'll be set. "yyyy/mm/dd" and "yyyymmdd" work just fine too. (For a better phrasing on "sortable" date formats check out RFC 3339 section 5.1.)

A reason to use "yyyy-mm-dd" format is because that's the format that SQLite's builtin date uses.

Solution 7 - Sql

Special thanks to Jeff and vapcguy your interactivity is really encouraging.

Here is a more complex statement that is useful when the length between '/' is unknown::

SELECT * FROM tableName
WHERE julianday(
    substr(substr(date, instr(date, '/')+1), instr(substr(date, instr(date, '/')+1), '/')+1)
||'-'||
    case when length(
    substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1),'/')-1)
    )=2
    then
    substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1), '/')-1)
    else
    '0'||substr(date, instr(date, '/')+1, instr(substr(date, instr(date, '/')+1), '/')-1)
    end
||'-'||
    case when length(substr(date,1, instr(date, '/')-1 )) =2
    then substr(date,1, instr(date, '/')-1 )
    else
    '0'||substr(date,1, instr(date, '/')-1 )
    end
) BETWEEN julianday('2015-03-14') AND julianday('2015-03-16') 

Solution 8 - Sql

Put the variable in the Where Condition and parse both dates using 'BETWEEN':

SELECT * FROM emp_master

-> if you have date formate like dd/mm/yyyy simple then,

   WHERE joined_date BETWEEN '01/03/2021' AND '01/09/2021';

-> and if you have date formate like yyyy/mm/dd then,

   WHERE joined_date BETWEEN '2021/03/01' AND '2021/09/01';

☻♥ Done Keep Code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZombianView Question on Stackoverflow
Solution 1 - SqlEric PetroeljeView Answer on Stackoverflow
Solution 2 - SqlUtku YıldırımView Answer on Stackoverflow
Solution 3 - SqlAFDView Answer on Stackoverflow
Solution 4 - SqlTROUZINE AbderrezaqView Answer on Stackoverflow
Solution 5 - SqladudakovView Answer on Stackoverflow
Solution 6 - SqlCaptain ManView Answer on Stackoverflow
Solution 7 - SqlTROUZINE AbderrezaqView Answer on Stackoverflow
Solution 8 - SqlNikhil S MaratheView Answer on Stackoverflow