Bergum
Bergum

Reputation: 77

Get SQL data between two dates when date field has time and date in it

I have data in my sql like: 2018-06-26 07:15:06 Right now I am using: $sqlq = "Select * from db WHERE date LIKE '$myDato' "; $myDato is set as: 2018-06-26% This works like a charm. I am getting all rows starting with that day. Now i want to extract more days. I have tried:

$sqlq = "Select * from db WHERE date BETWEEN >= '$myDato' AND => '$myDato2'";
$sqlq = "Select * from db WHERE date BETWEEN LIKE '$myDato' AND LIKE '$myDato2'";
$sqlq = "Select * from db WHERE date BETWEEN '$myDato' AND '$myDato2'";

but i can't find the right one.

Upvotes: 0

Views: 49

Answers (4)

Lovepreet Singh
Lovepreet Singh

Reputation: 4840

Try this:

$sqlq = "Select * from db WHERE date  >= '$myDato' AND date >= '$myDato2'";

If there is something like % in the end of date, then following solution can be used.

$sqlq = "Select * from db WHERE date  >= '" . substr($myDato, 0, -1) . "' AND date >= '" . substr($myDato2, 0, -1) . "'";

substr($myDato, 0, -1) will remove the last character from string.

Upvotes: 2

rawathemant
rawathemant

Reputation: 744

Use this:-

$sqlq = "Select * from db WHERE date  >= '".$myDato."' AND date <= '".$myDato2."' ";

Upvotes: 0

Alex Yu
Alex Yu

Reputation: 29

For example get sql data between '2018-06-26' and '2018-06-27'

SELECT * FROM db WHERE STR_TO_DATE(`date`, '%Y-%m-%d %H:%i:%s') BETWEEN STR_TO_DATE('2018-06-26', '%Y-%m-%d %H:%i:%s') AND STR_TO_DATE('2018-06-27', '%Y-%m-%d %H:%i:%s')

Upvotes: 0

venkatesh
venkatesh

Reputation: 141

Select * from db WHERE date >='2018-06-26 07:15:06' AND date< "2018-06-30 07:15:06";

Upvotes: 0

Related Questions