This bug appears on Microsoft SQL Server. I used Views + Date Views for filtering content by date range taken from URL arguments and got the following error:

SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '\'.

The sample query is:

SELECT node.[title] AS [node_title], node.[nid] AS [nid], node.[language] AS [node_language], node.[created] AS [node_created], 'node' AS field_data_field_event_date_node_entity_type
FROM 
{node} node
LEFT JOIN {field_data_field_event_date} field_data_field_event_date ON node.nid = field_data_field_event_date.entity_id AND (field_data_field_event_date.entity_type = 'node' AND field_data_field_event_date.deleted = '0')
WHERE ( (( ([node].[status] = '1') AND ([node].[type] IN  ('event_on_annual_calendar')) AND (DATEPART(yyyy-mm-dd\Thh:mi:ss, CAST(field_data_field_event_date.field_event_date_value2 as smalldatetime)) >= '2011-11-25T00:00:00' AND DATEPART(yyyy-mm-dd\Thh:mi:ss, CAST(field_data_field_event_date.field_event_date_value as smalldatetime)) <= '2011-11-25T00:00:00') )) )
ORDER BY node_created DESC

MSSQL cannot take date format strings in the DATEPART function (e.g. 'yyyy-mm-dd'), only separate identifiers for year, month, day etc.
I need this functionality and work on patch now. Any help on possible solutions will be greatly appreciated.

Comments

d.novikov’s picture

Status: Active » Needs review
StatusFileSize
new4.67 KB

I've noticed new fixes for Microsoft SQL date formatting in the latest dev but they seem to be incomplete. In addition MSSQL doesn't apply any formatting in DATEPART and DATENAME functions (e.g. 'year' doesn't differ from 'yyyy' and 'yy'; anyway it will return 'yyyy' format). I developed a version based solely on SQL. It can apply any format strings and doesn't need extra PHP wrappers. Patch is created against the current dev version.

The drawback of this patch are rather big and probably low queries.

d.novikov’s picture

Posted a new topic about the problem to the Drupal 8 core issue queue. Please share your thoughts if you want: http://drupal.org/node/1360656

web360’s picture

Works great.

Tested with date and time granularity in Views.

Installation profile: Drupal 7.9, Date 7.x-2.0-alpha4 ( with patches ), Views 7.x-3.0-rc1, running on Windows Server 2008 R2, IIS 7.5, MS SQL 2008.

karens’s picture

Status: Needs review » Fixed

OK, I committed this. It looks more well thought out than the previous code.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.