Where Statement

  • I want to do a "where statement" using "OR". When I try to run it, the right info does not come out. Can you see what I am doing wrong.

    thank you

    SELECT

    SV00300.Service_Call_ID

    ,SV00300.Service_Description

    ,SV00300.DATE1

    ,SV00300.LOCATNNM

    ,SV00300.CUSTNAME

    ,SV00300.CUSTNMBR

    ,SV00300.ADRSCODE

    ,SV00300.Type_Call_Short

    ,SV00300.Contract_Number

    ,DAM_TechEE_Info.FRSTNAME

    ,DAM_TechEE_Info.Technician

    FROM

    SV00300

    FULL OUTER JOIN WS30702

    ON SV00300.Service_Call_ID = WS30702.WS_Job_Number

    INNER JOIN DAM_TechEE_Info

    ON WS30702.EMPLOYID = DAM_TechEE_Info.EMPLOYID

    WHERE

    SV00300.DATE1 Between (@Start_Date) and (@End_Date)and SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.DATE1 Between (@Start_Date) and (@End_Date)and SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.DATE1 Between (@Start_Date) and (@End_Date)and SV00300.Contract_Number in (@Contract_Number)

  • You are missing parenthesis. But you can actually streamline this quite a bit. I think you are looking for something along these lines.

    WHERE SV00300.DATE1 Between (@Start_Date) and (@End_Date)

    and

    (

    SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.Contract_Number in (@Contract_Number)

    )

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • THANK YOU - this works when I am in design and go to run

    But does not work when I double click on the report and apply the parameters. I hit apply and nothing happens

  • lindavol (8/26/2013)


    THANK YOU - this works when I am in design and go to run

    But does not work when I double click on the report and apply the parameters. I hit apply and nothing happens

    information you are providing is not enough for us to give you the answer...

    please provide some more info on this...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • lindavol (8/26/2013)


    THANK YOU - this works when I am in design and go to run

    But does not work when I double click on the report and apply the parameters. I hit apply and nothing happens

    That actually may be that because the WHERE clause is now written correctly and there are actually no rows to return.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Why are you using IN and not = for these comparisons?

    SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.Contract_Number in (@Contract_Number)

    If any of these variables are a comma-delimited list, they won't be resolved by SQL Server in the way you are expecting. You'll need to rework the code to resolve the items in the list or alternatively use dynamic SQL.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I tried changing it to the = it lets me be able to do the dates and the contract number with no problem but if I want just dates and location it says I need to put something in the contract number also.

    WHERE SV00300.DATE1 Between (@Start_Date) and (@End_Date)and SV00300.LOCATNNM = (@LOCATNNM)

    OR(SV00300.DATE1 Between (@Start_Date) and (@End_Date)and SV00300.Contract_Number = (@Contract_Number))

  • ChrisM@Work (8/27/2013)


    Why are you using IN and not = for these comparisons?

    SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.Contract_Number in (@Contract_Number)

    If any of these variables are a comma-delimited list, they won't be resolved by SQL Server in the way you are expecting. You'll need to rework the code to resolve the items in the list or alternatively use dynamic SQL.

    I agree! You may have to handle comma-delimited fields using custom function. I had faced such issue where it works when I select a single value from parameter but doesn't when I select multiple.

  • Snigdha Vartak (8/28/2013)


    ChrisM@Work (8/27/2013)


    Why are you using IN and not = for these comparisons?

    SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.Contract_Number in (@Contract_Number)

    If any of these variables are a comma-delimited list, they won't be resolved by SQL Server in the way you are expecting. You'll need to rework the code to resolve the items in the list or alternatively use dynamic SQL.

    I agree! You may have to handle comma-delimited fields using custom function. I had faced such issue where it works when I select a single value from parameter but doesn't when I select multiple.

    I would recommend that you view the link in my signature about splitting strings. It will provide you with details for the "custom function" that will split your string super fast and easy.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (8/28/2013)


    Snigdha Vartak (8/28/2013)


    ChrisM@Work (8/27/2013)


    Why are you using IN and not = for these comparisons?

    SV00300.CUSTNMBR in (@CUSTNMBR)

    or SV00300.LOCATNNM in (@LOCATNNM)

    or SV00300.Contract_Number in (@Contract_Number)

    If any of these variables are a comma-delimited list, they won't be resolved by SQL Server in the way you are expecting. You'll need to rework the code to resolve the items in the list or alternatively use dynamic SQL.

    I agree! You may have to handle comma-delimited fields using custom function. I had faced such issue where it works when I select a single value from parameter but doesn't when I select multiple.

    I would recommend that you view the link in my signature about splitting strings. It will provide you with details for the "custom function" that will split your string super fast and easy.

    Thanks Sean!

Viewing 10 posts - 1 through 9 (of 9 total)

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