display image on a report based on two date - sql-server

I am using sql server 2005 reporting service to generate report base on a database. There are two columns which are datetime type ColumnA and ColumnB. The report would display a KPI image on this report by comparing these two columns.Below is the expression for selecting image
SWITCH(DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)<0,"kpi_r",
DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)>0,"kpi_g",
DateDiff("d",Fields!ColumnA.Value,Fields!ColumnB.Value)=0,"kpi_y")
For most of the records, the image is correct. Only for one record, the result is very strange.
For this record
ColumnA=2010-04-23 08:00:00 ColumnB=2010-04-22 17:00:00
It would display kpi_r, it displayed kpi_y. I have checked the value of DateDiff(d,Fields!ColumnA.Value,Fields!ColumnB.Value) in the SSMS, the value is -1. Why does it display kpi_y? Does anyone meet this problem before?
Best Regards,

The difference is that the SSMS DATEDIFF function counts the interval boundaries between the two dates whereas ReportBuilder counts the actual intervals. Within SSMS if you cross midnight you have triggered a day boundary so in your example you get -1. In ReportBuilder it is looking for 24 hours to be between the two values so you get 0. If you change the time on ColumnA to be '2010-04-23 17:00:00' you will see the value changes to -1 as you expected. For your comparison it would probably make sense to strip the time component from ColumnA and ColumnB when you do this SWITCH statement.

the above answer is spot on.
here's a few ways to strip time off a date depending on your preferences:
1. do it in RS: use and expression like dateserial(year(Fields!ColumnA.Value),month(Fields!ColumnA.Value), day(Fields!ColumnA.Value)) in your switch expression
2. do it in SQL: use an expression like cast(round(cast(ColumnA as float),0,1) as datetime) in your query

Related

Ssrs reports Windows 2008

I'm fairly new to SQL and I have been issued my first report to build. I have written an SQL query to give me a set of results that I would like to publish in a report.
I have unioned about 20 small queries all containing the correct amount of columns. One column is a misc column with about 15 different descriptions in (this is what I want to count).
I have uploaded my data set and now want to be able to choose a cell in my report to bring back a certain description.
At the minute I'm using
=count(fields!misc.values)
and it's giving me the whole count, about 200.
I would like to know if there is any kind of "where clause" (filter) which I can use to state which description results I want to bring back.
You can use am expression to count the misc.value you need. It will work like a count using a where clause:
=Sum(iif(Fields!misc.Value ="Some description",1,0))
Example:
For count the FSMethod with MethodOne as value I used this expression:
=Sum(iif(Fields!FSMethod.Value ="MethodOne",1,0))
Note the expression sums by 1 if the FSMethod.Value is MethodOne.
For count the rows with FSMethod column with MethodTwo value.
=Sum(iif(Fields!FSMethod.Value ="MethodTwo",1,0))
Let me know if this can help you.

SSAS 2012 DAX SamePeriodLastYear() with Date Dimension

I have a date dimension and a fact table. I have two measures:
WorkOrdersCount:=
count(
FactWorkOrderLifeCycle[Clientsid]
)
and
WorkOrdersLastYearCount:=
CALCULATE(
count(FactWorkOrderLifeCycle[Clientsid]),
SAMEPERIODLASTYEAR(DimDate[FullDate])
)
WorkOrdersCount is simple and works fine. I thought WorkOrdersLastYearCount would be simple as well, but I now realize I don't understand SAMEPERIODLASTYEAR().
My date dimension has a DateSID column containing an integer representation of date as YYYYMMDD. It has two recordkeeping rows with SIDs of -1 and -2 for unknown and TBD dates. I'm only using the -1 row in this solution. The data is stored in a SQL Server table and the FullDate column is a "date" type. The actual value is 1900-01-01.
My fact, FactWorkOrderLifecycle, has a field called InvoicedDateSID that can have a null value which I replace with -1.
No errors are thrown in Visual Studio or when processing the olap, but upon referencing the column in a pivot table I get the following error:
ERROR - CALCULATION ABORTED: Calculation error in measure
'FactWorkOrderLifeCycle'[WorkOrdersLastYearCount]: An invalid numeric
representation of a date value was encountered.
Things I've tried (not all make sense):
changed SID values to positive integers
changed date value in dimdate to 9999-12-31 instead of 1900-01-01 when I saw that DAX dates might start at 1900-03-01
adding other dimensions to the pivot first to see if the formula calculates correctly at all.
I'm a DAX noob and I'm not sure how to troubleshoot this. Any help is appreciated!
Make sure you calendar table is indeed using a Date data type.
Remove any time component of your dates.
Make sure there are no gaps and no duplicates in your Calendar table.
Make sure you are using fields from your Calendar table on the pivot, and NOT date related fields from your data table.

SQL Server Report Builder : Concatenate data type "date" and "time"

I am working with SQL Server Report Builder 2008 R2.
I have a dataset that contains DateEntry (date, null) and TimeStampAuto (time(7), null) columns. I am trying to write an expression for concatenating those two values, so I can put it under DATETIME column on the report table. I tried the following but it does not work. It displays "#Error."
=First(Fields!DateEntry.Value, "Report1) & " " & First(Fields!TimeStampAuto.Value, "Report1)
When I just put the first part of the expression shown above, the report displays the date with some random time value (5/1/2015 12:00:00 AM). However, I did not put any time value in DateEntry. I only put the dates.
When I put the second part of the expression shown above, the report displays correct time that the data has.
I don't know why I can not concatenate those two.
The system type of the DateEntry.Value will be a DateTime value which is the readon that your report is showing 5/1/2015 12:00 AM even though it is only a date field in the database.
You need to format the date to only include the day,month and year to remove the included default time of 12:00 am
you will probably need to check for null values in both your date and time fields. you will also need to format the time filed to exclude any default date information.

Reporting Services 3.0 Chart calling stored procedure with date time value , how to sort?

I have a Stored Procedure in SQL Server 2008. One of the values returned is:
SELECT DATEADD(mm, DATEDIFF(mm,0,e.Day), 0) AS [Month],
So I get values like: 2012-05-01 00:00:00.000
I want to display the month in the X axis for a chart report, obviously sorted chronologically. It would be great if I can display the month as "May-2012" or "2012 - May". Right now it displays the whole thing as 5/1/2012 12:00:00 AM and it is sorted alphabetically instead of by month.
When I check the Sorting it shows the field [Month] and order A to Z, so I assume it is not "seeing" it as a datetime value.
How do I get the sorting right?
How do I change the format to display the month as "May-2012" ?
Thanks in advance!
OK, work around:
I talked to my DBA, asked him to return a formatted field from the stored procedure, with values like "2012-05" and '2012-12' and sort by that field. Then I used that new field on the Report builder Chart, and remove all sorting.
Works for now, but still need to understand how to do it right for future reports.

How to display time in HH:MM format?

How to display a time HH:MM format?
Using SQL 2000
In my Database time column datatype is varchar
Example
Table1
Time
08:00:00
09:00:23
214:23:32
Here I want to take only 08:00, 09:00, 214:23
How to make a query for this condition?
Whilst you could choose to turn the varchar into a datetime and format it there, assuming you do not want rounding, you could could shortcut the process. (Assuming the time format in the varchar is consistent)
select left('08:00:00',5)
Edit : Question altered, now I would use
select substring('243:00:00', 1, len('243:00:00') - 3)
and replace the value I used with the appropriate field
Cheap and cheerful.
I think Andrew was onto a correct solution, just didn't address all of the possibilities:
SELECT LEFT(Time, LEN(TIME)-3)
should trim off the last 3 characters.
Now, if you want to round up, that's another story....

Resources