I have an application which grabs some data with the following sql:
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= :startTime0
AND starttime < :endTime0
AND fieldname = :fieldNameVal0
UNION ALL
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= :startTime1
AND starttime < :endTime1
AND fieldname = :fieldNameVal1
UNION ALL
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= :startTime2
AND starttime < :endTime2
AND fieldname = :fieldNameVal2
Debugging and examining the code the parameter values are:
startTime0=Wed Sep 12 22:00:00 BST 2012
endTime0=Wed Sep 12 23:00:00 BST 2012
hitPrices0=1
startTime1=Wed Sep 12 23:00:00 BST 2012
endTime1=Thu Sep 13 00:00:00 BST 2012
hitPrices1=1
startTime2=Thu Sep 13 00:00:00 BST 2012
endTime2=Thu Sep 13 01:00:00 BST 2012
hitPrices2=1
Then running the following SQL in SQL-server management studio provides the correct data:
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= '2012-09-12 22:00:00'
AND starttime < '2012-09-12 23:00:00'
AND fieldname = 1
UNION ALL
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= '2012-09-12 23:00:00'
AND starttime < '2012-09-13 00:00:00'
AND fieldname = 1
UNION ALL
SELECT Sum(fieldname) AS fieldNameHere,
Sum(Cast(error AS INT)) AS Error,
Sum(jsperror) AS JSPError,
Sum(linkerror) AS LinkError
FROM schemaname.sessiondata session
WHERE starttime >= '2012-09-13 00:00:00'
AND starttime < '2012-09-13 01:00:00'
AND fieldname = 1
Note: this is not a mistake with startDate and endDate, the parameters are different from the fields. startDate and end date parameters represent the startdatetime and enddatetime of that paticular interval. the startDate field represents the session start time.
The incorrect results returned in the app:
[null, null, null, null]
[2, 1, 0, 0]
[null, null, null, null]
and the correct results from sqlsms:
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
I cannot work out why this is?
I discovered that formatting the date object and passing as a string to set the parameter on the SQL fixed this problem.