Case statement and parameters in T-SQL

  • I cannot get the following sort to execute in my report. Can you give me a clue how to get optional selecting and sorting?

    WHERE

    Case @Option

    When 'S' then '(skills.sas_operator IN (@RangeStart))'

    Else '(skills.sas_operator Between @RangeStart and @RangeEnd)'

    END

    ORDER BY

    Case @SortOption

    When 'S' then 'skills.sas_operator, skills.sas_station'

    Else 'skills.sas_station,skills.sas_operator '

    END

  • Not sure this belongs in a BI forum,

    but I believe that since you've enclosed the variables between quotes, they become part of the string and they are not interpreted as variables.

    I'm not a T-SQL expert, but I think you have to do something like this (someone please correct me if I'm wrong):

    '(skills.sas_operator IN (' + @RangeStart + '))'

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • rhfishman

    Can you give me a clue how to get optional selecting and sorting?

    Use and IF ELSE statement (From Books On Line)

    IF Boolean_expression

    { sql_statement | statement_block }

    [ ELSE

    { sql_statement | statement_block } ]

    Also note the first link in my signature block. Follow the directions contained in it, and you will most likely get a tested answer to your question. As it is without the table definition, sample data and required results we are left guessing at what you really require.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply