在 SSRS 中格式化 phone 数字 - 在一个非常串联的语句中
Formatting a phone number in SSRS - within a very concatenated statement
我有两段代码
="GROUP ESCORT:" & " " & CHR(10) &
IIf(First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_escort_name.Value, "DataSet1"),
First(Fields!grp_escort_name.Value, "DataSet1"),
(First(Fields!grp_escort_name.Value, "DataSet1") & " " & CHR(10) & IIF((IsNothing(First(Fields!grp_escort_email.Value, "DataSet1"))),
First(Fields!grp_escort_phone.Value, "DataSet1"),
((First(Fields!grp_escort_email.Value, "DataSet1")) & " " & CHR(10) & First(Fields!grp_escort_phone.Value, "DataSet1")))))
一个非常复杂的嵌套语句,它说 - 如果领导姓名与护卫姓名相同,则只需打印护卫姓名,否则打印护卫姓名,然后评估电子邮件是否为空仅打印 phone --如果电子邮件不为空打印电子邮件和 phone,等等
现在我已经整理了一段代码(借助堆栈溢出上的其他代码来评估和格式化 phone 数字。
这两段代码独立运行时效果很好,但我尝试过组合使用,但无法让它们一起运行。
IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing,
IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")),
Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"),
First(Fields!grp_escort_phone.Value, "DataSet1")))
我希望保留最上面的逻辑,但 phone 数字(如果打印)将被格式化的部分替换。
我的建议是用 CASE 语句处理 SQL 中的一些逻辑,因为这样会更简洁且易于测试。但是,由于您正试图在 SSRS 中获得答案,所以您可以开始了。
当我必须将像这样的复杂语句放在一起时,我会使用文本编辑器提取常用字符串并用更简单的字符串替换它们。例如:
提取各种值并创建占位符:
LNAME = First(Fields!leader_lname.Value, "DataSet1")
ENAME = First(Fields!grp_ENAME.Value, "DataSet1")
THE_EMAIL = First(Fields!grp_escort_email.Value, "DataSet1")
THE_PHONE = First(Fields!grp_escort_phone.Value, "DataSet1")
然后用占位符替换原始逻辑中的值:
="GROUP ESCORT:" & " " & CHR(10) &
IIf ( LNAME = ENAME,
ENAME,
ENAME & " " & CHR(10) & IIF(IsNothing(THE_EMAIL), THE_PHONE, (THE_EMAIL & " " & CHR(10) & THE_PHONE))
)
这应该会让事情更容易阅读。例如,您有几组额外的 ( 和 )。
然后您只需在一个地方更改您的值。在这种情况下,将 THE_PHONE 替换为您的逻辑:
THE_PHONE = IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1")))
使用您的搜索和替换功能将您的占位符放回原处,您将获得:
="GROUP ESCORT:" & " " & CHR(10) &
IIf ( First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_ENAME.Value, "DataSet1"),
First(Fields!grp_ENAME.Value, "DataSet1"),
First(Fields!grp_ENAME.Value, "DataSet1") & " " & CHR(10) & IIF(IsNothing(First(Fields!grp_escort_email.Value, "DataSet1")), IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1"))), (First(Fields!grp_escort_email.Value, "DataSet1") & " " & CHR(10) & IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1")))))
)
我无法对此进行测试,但即使最终输出错误,希望该过程对您有所帮助。
这应该是直截了当的,只需用 phone 数字替换该部分——用格式化版本的字符串。
="GROUP ESCORT:" & " " & CHR(10) &
IIf(First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_escort_name.Value, "DataSet1"),
First(Fields!grp_escort_name.Value, "DataSet1"),
(First(Fields!grp_escort_name.Value, "DataSet1") & " " & CHR(10) & IIF((IsNothing(First(Fields!grp_escort_email.Value, "DataSet1"))),
(Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####")),
((First(Fields!grp_escort_email.Value, "DataSet1")) & " " & CHR(10) & (Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"))))))
我有两段代码
="GROUP ESCORT:" & " " & CHR(10) &
IIf(First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_escort_name.Value, "DataSet1"),
First(Fields!grp_escort_name.Value, "DataSet1"),
(First(Fields!grp_escort_name.Value, "DataSet1") & " " & CHR(10) & IIF((IsNothing(First(Fields!grp_escort_email.Value, "DataSet1"))),
First(Fields!grp_escort_phone.Value, "DataSet1"),
((First(Fields!grp_escort_email.Value, "DataSet1")) & " " & CHR(10) & First(Fields!grp_escort_phone.Value, "DataSet1")))))
一个非常复杂的嵌套语句,它说 - 如果领导姓名与护卫姓名相同,则只需打印护卫姓名,否则打印护卫姓名,然后评估电子邮件是否为空仅打印 phone --如果电子邮件不为空打印电子邮件和 phone,等等
现在我已经整理了一段代码(借助堆栈溢出上的其他代码来评估和格式化 phone 数字。
这两段代码独立运行时效果很好,但我尝试过组合使用,但无法让它们一起运行。
IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing,
IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")),
Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"),
First(Fields!grp_escort_phone.Value, "DataSet1")))
我希望保留最上面的逻辑,但 phone 数字(如果打印)将被格式化的部分替换。
我的建议是用 CASE 语句处理 SQL 中的一些逻辑,因为这样会更简洁且易于测试。但是,由于您正试图在 SSRS 中获得答案,所以您可以开始了。
当我必须将像这样的复杂语句放在一起时,我会使用文本编辑器提取常用字符串并用更简单的字符串替换它们。例如:
提取各种值并创建占位符:
LNAME = First(Fields!leader_lname.Value, "DataSet1") ENAME = First(Fields!grp_ENAME.Value, "DataSet1") THE_EMAIL = First(Fields!grp_escort_email.Value, "DataSet1") THE_PHONE = First(Fields!grp_escort_phone.Value, "DataSet1")
然后用占位符替换原始逻辑中的值:
="GROUP ESCORT:" & " " & CHR(10) & IIf ( LNAME = ENAME, ENAME, ENAME & " " & CHR(10) & IIF(IsNothing(THE_EMAIL), THE_PHONE, (THE_EMAIL & " " & CHR(10) & THE_PHONE)) )
这应该会让事情更容易阅读。例如,您有几组额外的 ( 和 )。
然后您只需在一个地方更改您的值。在这种情况下,将 THE_PHONE 替换为您的逻辑:
THE_PHONE = IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1")))
使用您的搜索和替换功能将您的占位符放回原处,您将获得:
="GROUP ESCORT:" & " " & CHR(10) & IIf ( First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_ENAME.Value, "DataSet1"), First(Fields!grp_ENAME.Value, "DataSet1"), First(Fields!grp_ENAME.Value, "DataSet1") & " " & CHR(10) & IIF(IsNothing(First(Fields!grp_escort_email.Value, "DataSet1")), IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1"))), (First(Fields!grp_escort_email.Value, "DataSet1") & " " & CHR(10) & IIF(First(Fields!grp_escort_phone.Value, "DataSet1") Is Nothing, Nothing, IIF(IsNumeric(First(Fields!grp_escort_phone.Value, "DataSet1")), Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"), First(Fields!grp_escort_phone.Value, "DataSet1"))))) )
我无法对此进行测试,但即使最终输出错误,希望该过程对您有所帮助。
这应该是直截了当的,只需用 phone 数字替换该部分——用格式化版本的字符串。
="GROUP ESCORT:" & " " & CHR(10) &
IIf(First(Fields!leader_lname.Value, "DataSet1") = First(Fields!grp_escort_name.Value, "DataSet1"),
First(Fields!grp_escort_name.Value, "DataSet1"),
(First(Fields!grp_escort_name.Value, "DataSet1") & " " & CHR(10) & IIF((IsNothing(First(Fields!grp_escort_email.Value, "DataSet1"))),
(Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####")),
((First(Fields!grp_escort_email.Value, "DataSet1")) & " " & CHR(10) & (Format(Val(First(Fields!grp_escort_phone.Value, "DataSet1")), "(###) ###-####"))))))