How To Simplify Code Structure

  • How can the following code be rewritten so it looks like a list rather than repeating Not Like ...

    HAVING (NOT (MGPPG.Code LIKE 'RSD')) AND (NOT (MGPPG.Code LIKE 'LIT')) AND (NOT (MGPPG.Code LIKE 'MISC')) AND (NOT (MGPPG.Code LIKE 'CLO')) AND (NOT (MGPPG.Code LIKE 'POP')) AND (NOT (MGPPG.Code LIKE 'B')) AND ...

    Tried (NOT (MGPPG.Code LIKE ('RSD', 'LIT')) got an error. Tried (MGPPG.Code NOT LIKE ('RSD', 'LIT')) and got an error. Both errors mentioned missing parenthesis.

    SQL 2008 R2

    Thank you!

  • Use NOT IN ().

    Why are you using LIKE?


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Thank you, Not In worked. I was using Like because that is how the code was created on the SQL server when I created this as a new view. I then just copied the resulting code over into the PowerPivot report I'm working on. There are not a lot of lines generated by this particular report, so rather than create a view in SQL and link PP to it, I'm just writing it in the PP report itself.

    I'm new to SQL code, having worked in Access for most of the time. I still usually first create the report in Access, then look at the SQL code generated so I can write the SQL code. Get stuck on the SQL syntax once in a while... Also still working on understanding where Like should be used versus In.

    Thanks again for the help

  • Any time. Have a look at LIKE in Books Online when you get the time, it's tremendously useful.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

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

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