14

I want to select my data by date - from a date until another date, so I have this query,

SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09' AND '2014-10-10'

But this query only return the data in '2014-10-09', excluding the data in '2014-10-10', unless I change the query to this below,

SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09' AND '2014-10-11'

This is not an ideal solution. How can I select the data including the data in '2014-10-10'?

NOTE:

I think my problem is different from other duplicate questions becos,

  1. My date type is TEXT.
  2. I need to select the date's data without its time.
  3. It is an SQLite database.

My sample data:

    sid     nid timestamp   date    
1   20748   5   1412881193  2014-10-09 14:59:53 
2   20749   5   1412881300  2014-10-09 15:01:40 
3   20750   5   1412881360  2014-10-09 15:02:40
William Miller
  • 9,839
  • 3
  • 25
  • 46
Run
  • 54,938
  • 169
  • 450
  • 748
  • what field type is "date"? – dmgig Apr 30 '15 at 15:29
  • 1
    `'2014-10-11'` means midnight of the 10th. As in the first minute of the 10th. To be inclusive of the 10th you either need to add the time (`11:59`) or make it the 11th. – crthompson Apr 30 '15 at 15:30
  • the date type is TEXT... – Run Apr 30 '15 at 15:32
  • Yikes. That's kind of ... not great for a date. Do you get different kinds of date formats in there? – dmgig Apr 30 '15 at 15:32
  • possible duplicate of [SQL query to select dates between two dates](http://stackoverflow.com/questions/5125076/sql-query-to-select-dates-between-two-dates) – Martin Apr 30 '15 at 15:33
  • possible duplicate of [How do I query between two dates using MySQL?](http://stackoverflow.com/questions/3822648/how-do-i-query-between-two-dates-using-mysql) – isalgueiro Apr 30 '15 at 15:35
  • @dgig yeah I know. But I am not the one who designs the db. I don't have the power to change it... – Run Apr 30 '15 at 15:40
  • 2
    date type is TEXT **MAKES NO SENSE** do you mean your dates are stored as varchars? – Hogan Apr 30 '15 at 15:45
  • 2
    I understand, it happens. Well, if you do get different date types, you can make them regular dates with DATE_FORMAT(date,'%m-%d-%Y') – dmgig Apr 30 '15 at 15:46
  • @Hogan it is stored as text I guess. I am not the person who designed it. I don't store the data either. My part is to query it... – Run Apr 30 '15 at 15:51

4 Answers4

25

IF date is a timestamp, you'll need to do like:

SELECT * FROM mytalbe WHERE date BETWEEN '2014-10-09 00:00:00' AND '2014-10-10 23:59:59'

Or you can do, I believe:

SELECT * FROM mytalbe WHERE DATE(date) BETWEEN '2014-10-09' AND '2014-10-10'

Or, since it is a text field:

SELECT * FROM mytalbe WHERE DATE_FORMAT(date,'%Y-%m-%d') BETWEEN '2014-10-09' AND '2014-10-10'
dmgig
  • 4,400
  • 5
  • 36
  • 47
  • 1
    That second query is the right answer if you add your conversion to a datetime comment from above – crthompson Apr 30 '15 at 15:48
  • Thanks for the update. I get this error though `Error in sqliteSendQuery(con, statement, bind.data) : error in statement: no such function: DATE_FORMAT` – Run Apr 30 '15 at 15:55
  • ... sqlite? why is the question tagged with mysql – pala_ Apr 30 '15 at 15:56
  • My database is SQLite database. does it make any difference between MySQL? – Run Apr 30 '15 at 15:56
  • @pala_ I thought the querying syntax in sqlite is the same as the mysql. my apologies. – Run Apr 30 '15 at 15:57
  • ah, that I don't know exactly, but appears to be similar - for "date" at least: http://www.sqlite.org/lang_datefunc.html – dmgig Apr 30 '15 at 15:58
  • 1
    but this works anyway - `SELECT * FROM mytalbe WHERE DATE(date) BETWEEN '2014-10-09' AND '2014-10-10'` – Run Apr 30 '15 at 15:58
  • 2
    you could have mentioned from the very beginning that you also had a timestamp field :/ – pala_ Apr 30 '15 at 15:59
  • @teelou, I second pala's comment. Very frustrating not to see the full picture on what would otherwise be a very simple answer. – crthompson Apr 30 '15 at 16:00
  • @paqogomez my apologies. I tend to ignore the timestamp fields as they are difficult to decipher without converting them. – Run Apr 30 '15 at 16:03
11

You could also just not use between.

select * from mytable where `date` >= '2014-10-09' and `date` <= '2014-10-10'

Example:

mysql> create table dd (id integer primary key auto_increment, date text);
Query OK, 0 rows affected (0.11 sec)

mysql> insert into dd(date) values ('2014-10-08'), ('2014-10-09'), ('2014-10-10'), ('2014-10-11');
Query OK, 4 rows affected (0.05 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from dd where date >= "2014-10-09" and date <= "2014-10-10";
+----+------------+
| id | date       |
+----+------------+
|  2 | 2014-10-09 |
|  3 | 2014-10-10 |
+----+------------+
2 rows in set (0.01 sec)

Since it includes time, and you dont want the time. this:

select substring(date, 1, 10) from dd where substring(date, 1, 10) between '2014-10-09' and '2014-10-10';

question updated again, additional answer

Ugh. you have timestamp fields? in that case this:

select date(from_unixtime(timestamp)) from mytabel where date(from_unixtime(timestamp)) between '2014-10-09' and '2014-10-10'

finally we have arrived at sqlite

select date(datetime(timestamp, 'unixepoch')) 
  from mytable 
    where date(datetime(timestamp, 'unixepoch')) 
      between '2014-10-09' and '2014-10-10';
pala_
  • 8,901
  • 1
  • 15
  • 32
  • Thanks. I have tried it but I still don't get the data in '2014-10-10'. I think the problem is the date type is TEXT... – Run Apr 30 '15 at 15:40
  • @teelou Then can you please post your table data in the question, because as you can see from the above example, the query works as expected for the dates in a text field – pala_ Apr 30 '15 at 15:43
  • this answer does the same thing with a datetime type -- you want the OP to cast to Date – Hogan Apr 30 '15 at 15:44
  • @pala_ the date's data contains time info but I don't want to select the time. – Run Apr 30 '15 at 15:45
  • @teelou so just take the first 10 characters via substring – pala_ Apr 30 '15 at 15:46
  • @pala_ thanks for the answer. I think the answer of dgig solves my problem. – Run Apr 30 '15 at 15:48
  • @teelou okay. i just added a query that will work for you also - the time portion was the issue but you hadn't mentioned it until just now. Also, you could have mentioned you had timestamp fields – pala_ Apr 30 '15 at 15:53
  • @pala_ thanks for the update. yeah I have that timestamp fields but I tend to ignore it as they are difficult to decipher without converting them. – Run Apr 30 '15 at 16:00
  • @pala_ mind you. my database is sqlite. so it may be different from mysql. I updated my question. – Run Apr 30 '15 at 16:01
  • that may have something to do with it. i honestly dont know if sqlite supports `from_unixtime`, which is how you work with those sorts of fields in mysql – pala_ Apr 30 '15 at 16:02
  • `error in statement: no such function: from_unixtime` sqlite is sooooo backward! arg.... – Run Apr 30 '15 at 16:07
  • @pala_ yes that works finally! what does 'unixepoch' mean btw? – Run Apr 30 '15 at 16:15
  • 1
    @teelou it essentially means unix time - ie, treat the timestamp as seconds since the unix epoch, which is 1970-01-01 00:00:00 – pala_ Apr 30 '15 at 16:16
  • 1
    @teelou no problems. i'm guessing at least one of the answers here solved your question so you should probably accept one of them – pala_ Apr 30 '15 at 16:28
  • Thank you this is the perfect solution +1 – Harin Kaklotar Jun 13 '21 at 15:41
  • Not using between worked for me (But had to remove time). – Leandro Gamarra Jul 28 '21 at 16:30
2

same problem solved, thanks. my code :

var FechaInicio = dtpFechaInicial.Value.ToString("yyyy-MM-dd");
var FechaFinal = dtpFechaFinal.Value.ToString("yyyy-MM-dd");

string SQLcmd = $"SELECT * FROM sorteos WHERE DATE(fecha) BETWEEN ('{FechaInicio}') AND ('{FechaFinal}')";
Toni A.
  • 21
  • 2
0

I tested this code snippet:

SELECT local_date FROM (SELECT '2014-10-09 14:59:53' local_date UNION SELECT '2014-10-09 15:01:40' UNION SELECT '2014-10-09 15:02:40') WHERE local_date BETWEEN date('2014-10-09') AND date('2014-10-10')

Which basically returned:

2014-10-09 14:59:53
2014-10-09 15:01:40
2014-10-09 15:02:40

So the query you're looking for is probably this:

SELECT * FROM mytalbe WHERE date BETWEEN date('2014-10-09') AND date('2014-10-10')

I highly recommend this official documentation: https://www.sqlite.org/lang_datefunc.html

postfixNotation
  • 181
  • 1
  • 5