Date Parameters for dynamic PIVOT where clause

Posted on 2011-03-16
Last Modified: 2012-05-11
I have  following query where I need to pass Dates parameters to the WHERE clause.
If I input the dates to the WHERE clause as Strings, then the records don't get filtered based on the dates. If I input the dates as DateTime, I get error saying 'Conversion failed when converting datetime from character string'.

Any suggestions?
Thanks in advance:

DECLARE @StartDate as DateTime,@EndDate as DateTime,@StartDateStr as varchar,@EndDateStr as varchar,@cols NVARCHAR(2000),@PIVOTcols NVARCHAR(2000),
@SQL nvarchar(max),@ProductID as varchar(max)

SET @productID = 9999
SET @StartDate = '04/11/2009'
SET @EndDate = '04/14/2009'
SET @StartDateStr = '04/11/2009'
SET @EndDatestr = '04/14/2009'

SELECT  @cols = 'A,B,C'

SELECT  @PIVOTcols = 'A,B,C'


'Select OrderID,ProductId,OrderDate,ShipDate, '  + @cols +
,convert(varchar,O.OrderDate,101) as OrderDate
,convert(varchar,O.ShipDate,101) as ShipDate
from OP
inner join O on OP.OrderId = O.ID
Inner join od on O.ID = od.orderid
Inner join odt on odt.OrderDetailID = od.ID
'AND convert(varchar,o.ShipDate,101) >= convert(varchar,' + @StartDate + ',101)
AND convert(varchar,o.ShipDate,101) <= convert(varchar,' + @EndDate +',101)
(max(TA) for ST in (' + @PIVOTcols + ')) as PivotTable '

Question by:IT-CICA
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 33

Expert Comment

ID: 35152154
try varchar(10) and format 121:

AND convert(varchar(10),o.ShipDate,121) >= convert(varchar(10),' + @StartDate + ',121)
AND convert(varchar(10),o.ShipDate,121) <= convert(varchar(10),' + @EndDate +',121)
LVL 33

Accepted Solution

knightEknight earned 500 total points
ID: 35152157
oops, i see extra quote marks ...

AND convert(varchar(10),o.ShipDate,121) >= convert(varchar(10), @StartDate, 121)
AND convert(varchar(10),o.ShipDate,121) <= convert(varchar(10), @EndDate, 121)
LVL 33

Expert Comment

ID: 35152169
using format 101 the string comparisons will not work properly because (for example) april of 2010 will sort before march of 2011 because the month is first.

  04/01/2010 > 03/01/2011

Format 121 puts the year first, resolving this issue

  2011-03-01 > 2010-04-01

Expert Comment

ID: 35152205
'...Inner join odt on odt.OrderDetailID = od.ID
AND o.ShipDate >= convert(datetime,''' + @StartDate + ''',101)
AND o.ShipDate <= convert(datetime,''' + @EndDate +''',101) PIVOT...'

Author Closing Comment

ID: 35332783

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Syntax to get a value associated with most recent date 16 61
What type of testing am I doing? 4 74
Queries: Select, then Append, then Delete 8 33
invoke-sqlcmd help 5 26
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question