[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:
SELECT * FROM "My_table"
WHERE (Condition1 = 1 AND Condition2 = 2) OR
((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. :ehh:
Thank you for your help and sorry if I am in the wrong place...
Have a good day!
Re: SQL SELECT with multiple condition (SQL Server 2017)
You seem to be missing a close bracket.
Re: SQL SELECT with multiple condition (SQL Server 2017)
Quote:
Originally Posted by
Macronaute
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
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:
WHERE (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 = ''))
And it give me that :
SQL Code:
WHERE (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 = '')
I did not understand why.:ehh:
Re: SQL SELECT with multiple condition (SQL Server 2017)
Thank you, I looked where to post it this morning but I didn't find !
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
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', '')
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 :bigyello:
Re: SQL SELECT with multiple condition (SQL Server 2017)
Quote:
Originally Posted by
si_the_geek
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!