Results 1 to 9 of 9

Thread: [RESOLVED] SQL SELECT with multiple condition (SQL Server 2017)

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Resolved [RESOLVED] SQL SELECT with multiple condition (SQL Server 2017)

    Hi, I'm sorry to post it here, but I didn't find a place for SQL issues.

    I'm trying to extract values from my SQL table for my vb.net app but I have some troubles...

    I am trying to do that :

    SQL Code:
    1. SELECT * FROM "My_table"
    2. WHERE (Condition1 = 1 AND Condition2 = 2) OR
    3. ((Condition1 = 2 AND Condition2 = 1) AND (Condition3 =1 OU Condition3 = 2 OU Condition3 = 3)

    But when I try it, SQL change the place of my parentheses.

    Thank you for your help and sorry if I am in the wrong place...
    Have a good day!

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    646

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    You seem to be missing a close bracket.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,829

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    Quote Originally Posted by Macronaute View Post
    Hi, I'm sorry to post it here, but I didn't find a place for SQL issues.
    That would be the Database Development section... I've notified the moderators...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Question Re: SQL SELECT with multiple condition (SQL Server 2017)

    I'm sorry, I did it right on SQL Server, but when I copied it in the forum I missed it.

    So to be clearer, I write that :

    SQL Code:
    1. WHERE        (Soldee IS NULL AND Resp_MET = 'TEST' AND Emet <> 'init' AND CPRC = 'NON') OR
    2.                          ((Soldee IS NULL AND Resp_MET = 'TEST' AND Emet <> 'init' AND CPRC = 'OUI') AND (Decision_Q = 'OKAY' OR  Decision_Q = 'REFU' OR  Decision_Q = ''))

    And it give me that :

    SQL Code:
    1. WHERE        (Soldee IS NULL) AND (Resp_MET = 'TEST') AND (Emet <> 'init') AND (CPRC = 'NON') OR
    2.                          (Soldee IS NULL) AND (Resp_MET = 'TEST') AND (Emet <> 'init') AND (CPRC = 'OUI') AND (Decision_Q = 'OKAY' OR Decision_Q = 'REFU' OR Decision_Q = '')

    I did not understand why.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    Thank you, I looked where to post it this morning but I didn't find !

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,829

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    What are you using to write your queries? SQL Server Management Studio (SSMS)? If so, then you must have a plugin or something because SSMS by itself doesn't change queries.

    Code:
     (Soldee IS NULL AND Resp_MET = 'TEST' AND Emet <> 'init' AND CPRC = 'NON') OR
    ((Soldee IS NULL AND Resp_MET = 'TEST' AND Emet <> 'init' AND CPRC = 'OUI') AND (Decision_Q = 'OKAY' OR  Decision_Q = 'REFU' OR  Decision_Q = ''))
    
    
    (Soldee IS NULL) AND (Resp_MET = 'TEST') AND (Emet <> 'init') AND (CPRC = 'NON') OR
    (Soldee IS NULL) AND (Resp_MET = 'TEST') AND (Emet <> 'init') AND (CPRC = 'OUI') AND (Decision_Q = 'OKAY' OR Decision_Q = 'REFU' OR Decision_Q = '')
    It looks like what ever it is, is removing the outer parens from the or clause... but I think the whole thing can be simplified anyways...

    Code:
    ( (Soldee IS NULL) AND (Resp_MET = 'TEST') AND (Emet <> 'init') ) AND 
      ( 
        (CPRC = 'NON') OR
        ( 
          (CPRC = 'OUI') AND (Decision_Q = 'OKAY' OR Decision_Q = 'REFU' OR Decision_Q = '') 
        ) 
      )
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,430

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    Thread moved to the 'Database Development' forum


    This bit:
    Code:
    AND (Decision_Q = 'OKAY' OR Decision_Q = 'REFU' OR Decision_Q = '')
    ...can be simplified to:
    Code:
    AND Decision_Q IN('OKAY', 'REFU', '')

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    Thank you for your return, but I just resolve my issue and I am a bit ashamed because of my mistake.

    To explain, I was writing in the SQL Server Management Studio (SSMS) but in the "Edit 200 rows" (You can show the SQL pane to wright queries).
    Now, I taped it in the "Select 1000 rows" and it works perfectly !

    Thank you and excuse me for my silly question

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Re: SQL SELECT with multiple condition (SQL Server 2017)

    Quote Originally Posted by si_the_geek View Post
    Thread moved to the 'Database Development' forum


    This bit:
    Code:
    AND (Decision_Q = 'OKAY' OR Decision_Q = 'REFU' OR Decision_Q = '')
    ...can be simplified to:
    Code:
    AND Decision_Q IN('OKAY', 'REFU', '')
    Thank you for it too!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width