简体   繁体   中英

Formatting Date Time for SSRS (SQL Server 2008 R2)

I want to draw charts from previously saved sensor data from database based on start date and end date parameters provided by user. I show user calendar icon and default start date ( =DateAdd("d",-2,Today()) ) and end date today ( =DateAdd("d", 0,Today()) ).

Calendar icon shows date as 11.6.2016 but in database the date is saved as 2016-06-11 00:00:05.217 . If I use the following SQL query nothing is returned but if I use the same in visual studio query designer then I get expected data. What would be the correct syntax for it?

SELECT Timestamp, Value, FieldSensorName
FROM MeasurementTable
WHERE (FieldSensorName IN (@ReportParameterSensorName)) 
  AND (Timestamp > CONVERT(varchar, @ReportParameterStartDate, 121)) 
  AND (Timestamp < CONVERT(varchar, @ReportParameterEndDate, 121))

I am passing selected sensor names ( @ReportParameterSensorName ) from the UI with multiple selection list box.

Your problem could be that you are trying to compare a Timestamp by a greater than with a VARCHAR

AND (Timestamp >  CONVERT(varchar, @ReportParameterStartDate, 121))
AND (Timestamp < CONVERT(varchar, @ReportParameterEndDate, 121))

The CONVERT(varchar,... actually converts the value to a varchar rather than a date. Your parameters are already a date so you shouldn't need to cast or convert them. If Timestamp is a DATETIME field you should just be able to do something like:

AND (Timestamp >  @ReportParameterStartDate)
AND (Timestamp < @ReportParameterEndDate)

Although your parameters will not have hours/minutes/seconds it will still look for everything up after midnight on the Start parameter date and before midnight of the End parameter date. If you intend on the dataset also including the end day you can alter your parameter for end date.

AND (Timestamp < DATEADD(MS,-3,CAST(CAST(@ReportParameterEndDate + 1 AS DATE) AS DATETIME)))

Is one way I normally get end of day. What it does is add a day to the end date cast it as a date to drop off the hours/minutes/seconds then casts it back to a datetime and deletes 3 milliseconds to get to the highest precision date at the end of day before rounding up to midnight the next day eg 2016-06-14 23:59:59.997.

 (FieldSensorName IN (@ReportParameterSensorName)) 

Could also be an issue depending on what you are passing in the @ReportParameterSensorName variable as it will be treated as a single literal value. So if you are passing a comma separated list or something then you need to lookup how to split the string into a table and use a join rather than an in list. Unless you are okay with some potential for error and just changing to something like

@ReportParameterSensorNameLIKE '%' + FieldSensorName '%'

Which will match any part of the string to the field for sensor name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM