Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 WHERE clause doesn't filter on date

Author  Topic 

meef
Posting Yak Master

113 Posts

Posted - 2011-09-07 : 14:51:46
I'm trying to use this WHERE clause:

WHERE     (shdt BETWEEN '2/1/11' AND '6/30/11') AND (acct = 'bur') AND (conm LIKE 'home%') OR
(conm LIKE 'wal%') OR
(conm LIKE 'lowe%') OR
(conm LIKE 'targ%') OR
(conm LIKE 'menard%') OR
(conm LIKE 'kmar%')


It will filter the date accordingly until I start adding the other conm fields there. Once I put the ORs in it just shows me every date instead of the range I try to specify. I know I've run into this before but forget how to fix it.

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-09-07 : 15:02:06
This is an operator precedence issue. The AND operation will be given higher precedence than the OR operators. You want to put parentheses around all of you OR terms.

=======================================
The first rule of Tautology Club is the first rule of Tautology Club. -xkcd, (Honor Societies)
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2011-09-07 : 15:16:53
Not sure I understand where else to put the parenthesis, I already have them around each OR statement...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 15:57:29
(
(conm LIKE 'home%') OR
(conm LIKE 'wal%') OR
(conm LIKE 'lowe%') OR
(conm LIKE 'targ%') OR
(conm LIKE 'menard%') OR
(conm LIKE 'kmar%')
)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 15:58:25
Better yet as I hate all of the unnecessary ones:
(
conm LIKE 'home%' OR
conm LIKE 'wal%' OR
conm LIKE 'lowe%' OR
conm LIKE 'targ%' OR
conm LIKE 'menard%' OR
conm LIKE 'kmar%'
)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2011-09-07 : 16:17:51
quote:
Originally posted by tkizer

Better yet as I hate all of the unnecessary ones:
(
conm LIKE 'home%' OR
conm LIKE 'wal%' OR
conm LIKE 'lowe%' OR
conm LIKE 'targ%' OR
conm LIKE 'menard%' OR
conm LIKE 'kmar%'
)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



When I try that, the moronic program takes them away and just puts them around each line again like in my original post... or maybe I'm the moron, who knows, but it's not letting me do that.


EDIT: Apparently you need to make a new query to make it leave your syntax alone.

That didn't work either though, still getting dates from 2009.

WHERE     (shdt BETWEEN '2/1/11' AND '6/30/11') AND (acct = 'bur') AND (conm LIKE 'home%') OR
(conm LIKE 'home%' OR
conm LIKE 'wal%' OR
conm LIKE 'lowe%' OR
conm LIKE 'targ%' OR
conm LIKE 'menard%' OR
conm LIKE 'kmar%')
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 16:28:39
What program are you using?

You've got conm LIKE 'home%' outside of the parenthesis.


WHERE (shdt BETWEEN '2/1/11' AND '6/30/11' AND acct = 'bur') AND
(conm LIKE 'home%' OR
conm LIKE 'wal%' OR
conm LIKE 'lowe%' OR
conm LIKE 'targ%' OR
conm LIKE 'menard%' OR
conm LIKE 'kmar%')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-09-07 : 16:29:57
hard to say since you haven't given us any information with than your where clause. Maybe this?
WHERE
(shdt BETWEEN '2/1/11' AND '6/30/11')
AND (acct = 'bur')
AND
(
(conm LIKE 'home%') OR
(conm LIKE 'wal%') OR
(conm LIKE 'lowe%') OR
(conm LIKE 'targ%') OR
(conm LIKE 'menard%') OR
(conm LIKE 'kmar%')
)
If that doesn't work please post DDL, DML and Expected Output. This link can help you put that together:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2011-09-07 : 16:33:04
quote:
Originally posted by tkizer

What program are you using?

You've got conm LIKE 'home%' outside of the parenthesis.


WHERE (shdt BETWEEN '2/1/11' AND '6/30/11' AND acct = 'bur') AND
(conm LIKE 'home%' OR
conm LIKE 'wal%' OR
conm LIKE 'lowe%' OR
conm LIKE 'targ%' OR
conm LIKE 'menard%' OR
conm LIKE 'kmar%')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Whoops, looks like that was it and I just missed the other "conm" there. Eyes played tricks on me.

Thanks.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-07 : 16:36:05
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -