Complex conditions in SQL statements

# Thread: Complex conditions in SQL statements

1. Senior Member
Join Date
Dec 1969
Posts
604

## Complex conditions in SQL statements

How do you build complex conditions?<BR><BR>Eg SELECT * WHERE a=1 AND b=2 AND C=3 OR D=4 AND E=5<BR><BR>Do you use brackets?<BR><BR>eg SELECT * WHERE a=1 AND b=2 AND (C=3) OR (D=4 AND E=5)<BR><BR>??<BR><BR>

2. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## They are called parenthesis.

And, the answer is Yes.<BR><BR>It all depends on what you want to do.<BR><BR>Think back to simple arithmetic.<BR><BR>What does X equal?<BR>X = 5 + 2 * 10<BR><BR>70?<BR>25?<BR><BR>You use the parenthesis to denote order:<BR>X = (5 + 2) * 10<BR><BR>70.<BR><BR>Or:<BR>X = 5 + (2 * 10)<BR><BR>25.<BR><BR>Figure out HOW you want it to work. Then, add the parenthesis as needed.

3. Senior Member
Join Date
Dec 1969
Posts
11,334

I knew that would come in handy some day...

4. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Please Excuse My Dear Aunt Sara

Hey - DG. Got a question for you.<BR><BR>Over, out front of the main hospital is a sign that says:<BR>No smoking;<BR>No hats;<BR>No saluting;<BR>In the verdana.<BR><BR>First, I&#039;m guessing that the verdana is the big, roofed, open area -- pretty much the porch.<BR><BR>But, why the no saluting? Seems odd to me.

5. Senior Member
Join Date
Dec 1969
Posts
293

## RE: Complex conditions in SQL statements

Easiest is to build the query you want in Access. Then cut and paste the sql code to where you want it. It also allows for including variable replacement:<BR><BR>
Code:
`<BR>Built SQL:<BR> PersonID = "12345" <BR> FormName="Demographics"<BR> FullUserName="ITSME"<BR> sss = "SELECT LockTable.PersonID, LockTable.FormName, <BR> sss = sss & "LockTable.FullUserName AS Expr1"<BR> sss = sss & " FROM LockTable"<BR> sss = sss & " WHERE (((LockTable.PersonID)="<BR> sss = sss & chr(34) & PersonID & chr(34) & ")"<BR> sss = sss & " AND ((LockTable.FormName)="<BR> sss = sss & chr(34) & FormName & chr(34) & ")"<BR> sss = sss & " AND (([LockTable].[FullUserName])="<BR> sss = sss & FullUserName & chr(34) & "))"<BR><BR>Original SQL:<BR><BR>SELECT LockTable.PersonID, LockTable.FormName, <BR>LockTable.FullUserName AS Expr1<BR>FROM LockTable<BR>WHERE (((LockTable.PersonID)="12345") AND ((LockTable.FormName)<BR>="Demographics") AND (([LockTable].[FullUserName])="ITSME"));<BR><BR><BR>`
<BR>Generally speaking, if you cut the ending ; off, it becomes pretty standard SQL

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## A few reasons

No hats are required under a &#039;cover&#039; (since your hat is a cover) It could also be a &#039;no hat&#039; zone which allows work to be done.<BR><BR>Most doctors and lawyers have the rank of officer just for cushiness... they&#039;re non-combatants (meaning they have no power of commmand) You don&#039;t salute chaplins either, even though they&#039;re officers.<BR><BR>Also, and probably the reason... you&#039;re going to the hospital cause you&#039;re sick. Maybe not to injure ya any further..<BR><BR>Maybe they put the sign up to help the FNG&#039;s. (figure that acronmyn out)

7. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Except there are a..

.. handful of EXTRA parenthesis in there.<BR><BR>And, when you pass a query from ASP to a database, you have to use &#039; instead of ".<BR><BR>But, to each his own. Your way probably works, so I shouldn&#039;t knock it.

8. Senior Member
Join Date
Dec 1969
Posts
2,437

## RE: Complex conditions in SQL statements

That&#039;s far from standard SQL<BR><BR>chr is not a standard function<BR>& is not used for concatenation<BR>[] can not be used to embrace identifiers<BR>" can not be used as string delimiters<BR><BR>You are right about the ; though.

9. Senior Member
Join Date
Dec 1969
Posts
293

## RE: Except there are a..

You are definitely correct about the number of parenthesis. Do you think it causes significant processor overhead? I&#039;ve never done a time comparision, so I couldn&#039;t say for sure.<BR><BR>My guess has always been that any overhead was more than made up for by convience to me. That doesn&#039;t have to be the case, tho.

10. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## RE: Except there are a..

I imagine there is a teeny-weeny bit of overhead from it. I doubt that it is measurable.<BR><BR>But, I&#039;ll greatly disagree with you on the convience part. Sure, it makes it quicker to initially write the query - but it makes it heard to maintain and debug w/ all of the slop in there. And, you aren&#039;t actually learning SQL Syntax.<BR><BR>What are you going to do when presented with a MySql database? Or Oracle? Or Sql Server?<BR><BR>Throw away WYSIWYG tools and work by hand! I&#039;ve found that I can do most things as quick or quicker than using them, anyway.

#### Posting Permissions

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