Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS function with regex

I am creating a report using SSRS for a phone number field in my database as a string value. I need to format a string value in phone number format (555) 555-1212. If the value is null, display nothing.

For example, a table in my database has a phone number column and some values are NULL.

I got a regex that formats the phone number fine.

=System.Text.RegularExpressions.Regex.Replace(Fields!Phone.Value, "(\d{3})[ -.](\d{3})[ -.](\d{4})","($1) $2-$3")

However if I do this:

=IIf(Fields!Phone.Value is nothing, "", System.Text.RegularExpressions.Regex.Replace(Fields!Phone.Value,"(\d{3})[ -.](\d{3})[ -.](\d{4})","($1) $2-$3"))

Then it comes back with an error. #ERROR is displayed on my report. Can you use iif with a regex? Is there another way?

like image 981
JacksOrBetter99 Avatar asked Jun 30 '10 20:06

JacksOrBetter99


People also ask

How do you use expressions in SSRS report?

In Design view, click the text box on the design surface to which you want to add an expression. For a simple expression, type the display text for the expression in the text box. For example, for the dataset field Sales, type [Sales] . For a complex expression, right-click the text box, and select Expression.

Does SQL Server support regular expressions?

Regular expressions are a concise and flexible notation for finding and replacing patterns of text. A specific set of regular expressions can be used in the Find what field of the SQL Server Management Studio Find and Replace dialog box.

How do I use IIF in SSRS expression?

Using IIF Function in SSRS We are going to add a new field to the report data set to determine if the Order Year is the Max or Current Year. As shown below, the dataset properties window is opened, and the Fields tab is selected. After clicking Add, at the bottom of the list a new field is added.

How do you write if condition in SSRS report expression?

location. Value = “CA”, “Bold”, “Italic”) SSRS iif statement The format of the IIF() statement is as follows: =IIF(Expression, Condition set when the expression is true, Condition set when the expression is false) It should be a Boolean expression, according to parameter 1.


1 Answers

(I'm assuming that you're only getting #ERROR for your null-valued records, since that's what happened when I set up a test case using the expression you provided. If not, I'll investigate further.)

You certainly can use a regex inside of the IIF() statement, but it evaluates both result conditions regardless of the result of your test expression. Therefore, your Regex.replace() is performed on your NULL values as well, which naturally generates an error.

I was able to produce the behaviour I think that you're after by rearranging things a little, so that we have this expression instead (formatted for readability):

=System.Text.RegularExpressions.Regex.Replace(
  IIf(IsNothing(Fields!Phone.Value), "", Fields!Phone.Value),
  "(\d{3})[ -.](\d{3})[ -.](\d{4})",
  "($1) $2-$3")

That will give you a blank field when the value is NULL, otherwise it will format your telephone numbers accordingly. Hopefully that helps.

like image 132
Tim Stone Avatar answered Oct 06 '22 21:10

Tim Stone