简体   繁体   English

SSRS比较表达式中的日期

[英]SSRS comparing dates in an expression

I have some basic expressions to highlight certain fields: 我有一些基本表达式来突出显示某些字段:

=IIF(Fields!Failure.Value > 0, "Red", "Transparent")

However, I have another field that contains dates in the following format: 但是,我还有另一个字段,其中包含以下格式的日期:

22/08/2016 22/08/2016 - each field can contain multiple dates. 

This is stored in SQL as a VARCHAR. 这作为VARCHAR存储在SQL中。

I need to run a similar expression in my report to achieve the following: 我需要在报告中运行类似的表达式以实现以下目的:

If the date is 1 day older than the date the report is run, highlight the field. 如果日期比报表运行日期早1天,请突出显示该字段。 If the date is greater than 1 day older, highlight the field a different colour. 如果日期早于1天,请用另一种颜色突出显示该字段。

I#m familiar with basic expressions, but I can't think of an easy way to obtain the current date, and then compare between the two. 我已经熟悉基本表达式,但是我想不出一种简单的方法来获取当前日期,然后在两者之间进行比较。

You can use the Split function to generate an array of values. 您可以使用Split函数生成值数组。 Of course, you'll still need to select one of those to test. 当然,您仍然需要选择其中一项进行测试。 The following may get you going again. 以下内容可能会让您再次尝试。

=Iif(
    CDate(
        Split(
            "21/08/2016, 22/08/2016",
            ","
        ).GetValue(0)
    ) < Today,
    "True",
    "False"
)

If, however, you are dealing with a date string that can contain any number of dates and you to test all of them then a simple SSRS expression won't handle that. 但是,如果要处理的日期字符串可以包含任意数量的日期,并且要测试所有日期,那么简单的SSRS表达式将无法处理该日期字符串。 Fortunately we can use a bit of custom code. 幸运的是,我们可以使用一些自定义代码。

Right-click the report background and select report properties. 右键单击报告背景,然后选择报告属性。 Click the 'Code' item and paste the following into code box 点击“代码”项,然后将以下内容粘贴到代码框中

Public Function TestDate(DateString As String) As String
    Dim DatesArray() As String = Split(DateString)
    Dim ReturnValue As String = "Transparent"
    For Each d As String In DatesArray
        If Date.Parse(d) = DateAdd(DateInterval.Day, -1, Date.Today) Then
            ReturnValue = "Red"
        End If
        If Date.Parse(d) < DateAdd(DateInterval.Day, -1, Date.Today) Then
            ReturnValue = "DarkRed"
        End If
    Next
    Return ReturnValue
End Function

Now change the expression as below 现在如下更改表达式

=Code.TestDate("21/08/2016 22/08/2016")

I've used Date.Today in the VB to restrict the comparison of the date to the day. 我已经在VB中使用Date.Today来限制日期与当天的比较。 If you wanted to be more precise ie: the exact time, use Date.Now instead 如果您想更精确,即:确切的时间,请使用Date.Now代替

As others have said, you really shouldn't be using hacks like this... 正如其他人所说,您确实不应该使用这种黑客手段...

But, this should work for you: 但是,这应该为您工作:

=iif(Len(Replace(Replace(Fields!DateField.Value," ",""), Format(Today, "dd/MM/yyyy"),"")) = 0, "Transparent" ,iif(Len(Replace(Replace(Replace(Fields!DateField.Value," ",""), Format(Today, "dd/MM/yyyy"),""), Format(Today().AddDays(-1), "dd/MM/yyyy"),"")) = 0, "Green", "Red"))

Essentially, remove the joining character (in this case, space) and then replace all instances of the current date in the given format. 本质上,删除连接字符(在这种情况下为空格),然后以给定格式替换当前日期的所有实例。 If there are any characters left, you have a date that doesn't match today. 如果还剩下任何字符,那么您的日期与今天不匹配。 Then take that value and repeat for any instances of yesterday. 然后取该值,并在昨天的任何情况下重复。

Obviously this will fall down if your date formatting changes. 显然,如果您的日期格式更改,这将下降。
But then you already knew that comparing dates as strings was a bad idea, right... 但是您已经知道,将日期作为字符串进行比较是一个坏主意,对吧...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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