views:

255

answers:

1

Hi there,

I'm querying a mssql 2008 database via odbc and stubled upon a strange behaviour.

The following code is used...

$datefrom = "2009-06-01";
$dateto = "2009-07-01";
$clientno = "01";
$sth = $db->prepare("select count(*) from tbl WHERE v_valid_from <= ? and valid_from <= ? and (v_invalid_from >= ?) and (valid_to >= ? or valid_to is null) and clientno = ?");
$sth->execute(array($datefrom, $datefrom, $dateto, $dateto, $clientno));

the query retrieves 80 rows though 1000 were expected.

Looking in the profiler, the server executes the following statement:

select count(*) from tbl 
WHERE v_valid_from <= '20090601 00:00:00.000' and valid_from <= '20090601 00:00:00.000' and 
(v_invalid_from >= '20090701 00:00:00.000') and (valid_to >= '20090701 00:00:00.000' or valid_to is null) and clientno = '01'

when I put '-' in the dates, the query is executed properly.

There seems to be no special binding for datetime-values so I'm wondering why this happens.

The session settings are as following:

set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language English
set dateformat ymd
set datefirst 1
set transaction isolation level read committed
A: 

I don't have enough rep to comment. What happens if you change the '-' to '/' when you set the date values at the beginning? For instance:

$datefrom = "2009/06/01";
$dateto = "2009/07/01";
Randolph Potter