Adding an Else to your Switch

AID: 2279
  • Status: Published

12287 points

  • ByValentinoV
  • TypeTips/Tricks
  • Posted on2010-01-16 at 14:54:28
Awards
  • Community Pick
  • Experts Exchange Approved
In this short article I will be talking about two functions in the SQL Server Reporting Services (SSRS) function stack.  Those functions are IIF() and Switch().  And I'll be showing you how easy it is to add an Else part to the Switch function.

Two commonly-used functions in SQL Server Reporting Services are the IIF() and the Switch().  These are two functions of the Program Flow type, or Decision Functions as they are called on this MSDN page.

In case you're wondering why it's so difficult to find a function reference for the built-in functions of SSRS, it's because these are actually Visual Basic functions and Microsoft refers to those for any detailed explanation.  Their references are located at the bottom of this article.

Anyone who's done some programming most likely already knows the if <expression> then <some_code> else <other_code> statement.  If <expression> evaluates to true then <some_code> gets executed, else <other_code>  gets executed.

The IIF() works in the same way.  According to its description it:

Returns one of two objects, depending on the evaluation of an expression.


This is its definition:

Public Function IIf( _
    ByVal Expression As Boolean, _
    ByVal TruePart As Object, _
    ByVal FalsePart As Object _
) As Object
                                    
1:
2:
3:
4:
5:

Select allOpen in new window


 
Here's a simple example.

=IIf(Fields!YearlyIncome.Value >= 600,"High","Low")
                                    
1:

Select allOpen in new window



Using this expression, the "High" string is returned when the value of the YearlyIncome field is equal to or above 600, while the string "Low" is returned when the value is below 600.

Now have a look at the following example.  It has been nicely structured with indentation and line breaks to make reading easier.

=IIF
(
    Sum(Fields!LineTotal.Value) >= 100,
    "Violet",
    IIF
    (
        Sum(Fields!LineTotal.Value) < 25,
        "Transparent",
        "Cornsilk"
    )
)
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window



As you see, it shows a nested IIF inside another one.  Imagine that there were several more nestings and that line breaks were not used by the coder.  Would be a nightmare to read, right?

That's why the Switch() was invented.  The description for the Switch function reads:

Evaluates a list of expressions and returns an Object value corresponding to the first expression in the list that is True.


And this is the function definition:

Public Function Switch( _
    ByVal ParamArray VarExpr() As Object _
) As Object
                                    
1:
2:
3:

Select allOpen in new window


 
In Reporting Services, the VarExpr parameter is simply an even list of expressions and/or object references separated by commas.  Which comes down to something like this: Switch(<expr1>, val1, <expr2>, val2).

Here's a simple example:

=Switch
(
	Fields!State.Value = "OR", "Oregon",
	Fields!State.Value = "WA", "Washington"
)
                                    
1:
2:
3:
4:
5:

Select allOpen in new window



This expression says that if the value for the State field is "OR" then the Switch function will return "Oregon", and so on...

Now, to get to the point of this article, the Switch function does not contain an ELSE part like the IIF does.

But I wouldn't be writing this if there wasn't a workaround, would I?  If you read the Switch's description closely, it says that it will return the first expression in the list that is true.  So each expression is evaluated in the order that they are passed to the function.  To get ELSE-like behavior we would need an expression that evaluates to True but only when all other expressions are False.  So, why not use True as expression?  It's the simplest expression that I can think of and it does the works!

Have a look at the following, it's a rewrite of the last IIF example mentioned earlier.

=Switch
(
    Sum(Fields!LineTotal.Value) >= 100, "Violet",
    Sum(Fields!LineTotal.Value) < 25, "Transparent",
    True, "Cornsilk"
)
                                    
1:
2:
3:
4:
5:
6:

Select allOpen in new window



So, which one do you think is the most readable?  The IIF, or the Switch?  These are only simple examples that I've been using, imagine situations with ten or more possibilities.  Well, I think you've got my point by now.

Thank you for reading this article, and remember: Happy Reporting!  Don't forget the little YES button on the way out ;-)

Valentino.

Originally appeared on my website: http://blog.hoegaerden.be/2009/09/14/adding-an-else-to-your-switch

References
MSDN VS2008 VB.NET IIf Function
MSDN VS2008 VB.NET Switch Function
Asked On
2010-01-16 at 14:54:28ID2279
Tags

Reporting Services

,

SSRS

,

IIF

,

Switch

,

if

,

switch else

,

sql server

Topic

MS SQL Reporting

Views
9883

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top SSRS SQL Reporting Svc Experts

  1. ValentinoV

    226,820

    Guru

    20 points yesterday

    Profile
    Rank: Genius
  2. huslayer

    108,772

    Master

    20 points yesterday

    Profile
    Rank: Sage
  3. TempDBA

    54,871

    Master

    0 points yesterday

    Profile
    Rank: Sage
  4. Nicobo

    37,600

    0 points yesterday

    Profile
    Rank: Wizard
  5. santhimurthyd

    36,656

    0 points yesterday

    Profile
    Rank: Wizard
  6. SThaya

    31,119

    0 points yesterday

    Profile
    Rank: Master
  7. planocz

    20,826

    0 points yesterday

    Profile
    Rank: Genius
  8. sammySeltzer

    19,700

    0 points yesterday

    Profile
    Rank: Genius
  9. harish_varghese

    18,800

    0 points yesterday

    Profile
    Rank: Master
  10. lcohan

    17,827

    0 points yesterday

    Profile
    Rank: Genius
  11. TimHumphries

    13,046

    0 points yesterday

    Profile
    Rank: Wizard
  12. EugeneZ

    12,950

    0 points yesterday

    Profile
    Rank: Genius
  13. dtodd

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  14. jimhorn

    11,065

    0 points yesterday

    Profile
    Rank: Genius
  15. srikanthreddyn143

    9,900

    0 points yesterday

    Profile
    Rank: Guru
  16. jogos

    9,800

    0 points yesterday

    Profile
    Rank: Sage
  17. HainKurt

    9,732

    0 points yesterday

    Profile
    Rank: Genius
  18. mlmcc

    8,100

    0 points yesterday

    Profile
    Rank: Savant
  19. ScottPletcher

    7,500

    0 points yesterday

    Profile
    Rank: Genius
  20. mwvisa1

    6,501

    0 points yesterday

    Profile
    Rank: Genius
  21. CodeCruiser

    6,250

    0 points yesterday

    Profile
    Rank: Genius
  22. wdosanjos

    5,000

    0 points yesterday

    Profile
    Rank: Genius
  23. Emes

    4,750

    0 points yesterday

    Profile
    Rank: Wizard
  24. Buttercup1

    4,750

    0 points yesterday

    Profile
    Rank: Master
  25. mark_wills

    4,664

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame