Ozi Oz
Ozi Oz

Reputation: 289

issue with sql query finding records between dates in SQL

i have this query which i use in order to get info of records between two dates.

$start=$_REQUEST['from'];
$end=$_REQUEST['to'];
$startd = str_replace('/', '-', $start);
echo $start1=date('Ymd', strtotime($startd));
$endd = str_replace('/', '-', $end);
echo $end1=date('Ymd', strtotime($endd));
echo $data ="SELECT * from forma 
  WHERE DATE(data) BETWEEN '".$start1."' AND '".$end1."' ";
  $res = odbc_exec($connection, $data) or die(odbc_error());

I get this error :

]'DATE' is not a recognized built-in function name., SQL

But how can i solve this in SQL? data is a varchar type..

Thanks

Upvotes: 1

Views: 175

Answers (2)

Thomas De Laet
Thomas De Laet

Reputation: 21

You can use:

echo $data ="select * FROM forma WHERE STR_TO_DATE(data, '%d/%m/%Y') BETWEEN '".$start1."' AND '".$end1."' ; 

Also you can add an extra wrapping with the mysql function to create a date format you like. For that use date_format()

Upvotes: 1

Imanuel
Imanuel

Reputation: 3667

If data consists of Ymd formatted date strings, you should be able to to:

$data="SELECT * from forma WHERE data BETWEEN '".$start1."' AND '".$end1."'";

You can compare strings: '2012' > '2011' and 'b' > 'a' and, in your case, '20121214' > '20120701', for example.

Upvotes: 0

Related Questions