| Author |
Topic |
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-27 : 07:08:42
|
| so i have a table with id, pagename (which includes a category) and some other columnsthe following statement doesn't seem to work and i can't figure out whySELECT id, pagename, otherstuffFROM tblPagesWHERE (id LIKE '1') AND (pageName NOT LIKE 'Cat1%') OR (id LIKE '1') AND (pageName NOT LIKE 'Cat2%')Where Cat1 and Cat2 are category names, so if i have 3 entries all with id = 1 (i know it's a crap column name, too long to explain why it's called id). I'm going through every row and trying to exclude everything with a prefix of Cat1 or Cat2, id by id. it works if i take everything after the OR out so what's going wrong? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-27 : 07:12:43
|
[code]SELECT id, pagename, otherstuffFROM tblPagesWHERE id LIKE '1' AND pageName NOT LIKE 'Cat[12]%')[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-27 : 07:14:31
|
| sorry, i changed the names of my prefixes for this post, they're not cat1 and cat2, they're 2 totally different words |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-27 : 08:57:02
|
quote: Originally posted by neil_akoga sorry, i changed the names of my prefixes for this post, they're not cat1 and cat2, they're 2 totally different words
Peso's code should workYour code will work with these changesSELECT id, pagename, otherstuffFROM tblPagesWHERE ((id LIKE '1') AND (pageName NOT LIKE 'Cat1%')) OR((id LIKE '1') AND (pageName NOT LIKE 'Cat2%'))MadhivananFailing to plan is Planning to fail |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-27 : 09:12:44
|
| it might work if my categories were called cat1 and cat2 but these aren't my actual column names as i don't want to post them here for obvious reasons. i think renaming them to cat1 and cat2 for the purposes of my post confused the issue. , here is what i haveSELECT id, pageNameFROM tblPagesWHERE (id LIKE '1') AND (pageName NOT LIKE 'Dogs%') OR (id LIKE '1') AND (pageName NOT LIKE 'Cats%') |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-27 : 09:21:09
|
quote: Originally posted by neil_akoga it might work if my categories were called cat1 and cat2 but these aren't my actual column names as i don't want to post them here for obvious reasons. i think renaming them to cat1 and cat2 for the purposes of my post confused the issue. , here is what i haveSELECT id, pageNameFROM tblPagesWHERE (id LIKE '1') AND (pageName NOT LIKE 'Dogs%') OR (id LIKE '1') AND (pageName NOT LIKE 'Cats%')
Can you post some sample data with expected result?MadhivananFailing to plan is Planning to fail |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-27 : 09:28:14
|
| sure here's an example tableid, pageName1, Cats - How to grow your own1, Dogs - Why do they bark1, Fish - What to feed them2, Cats - Why do they purr2, Fish - Fins and gillsetcso i want to select everything from this table that doesn't have a prefix of cats or dogs on the pagename column. I'm using sql server 2008 and visual studio 2008 to write my query. I keep trying to fit my parentheses around each "(id like '1') and (pagename not like 'whatever%')" condition but it just removes the brackets every time. does this sound like a bug in visual studio's intellisense to you? It's basically not letting me do the followingselect * from table where(id like '1' and pagename like 'cats%') or (id like '1' and pagename like 'dogs%')it's also not letting me do the following parenthesesselect * from table where((id like '1') and (pagename like 'cats%')) or ((id like '1') and (pagename like 'dogs%')) |
 |
|
|
neil_akoga
Yak Posting Veteran
56 Posts |
Posted - 2009-04-27 : 09:31:09
|
| forgot to say that i'm looping through this table doing a select for each available id so i might do x amount of selects with x amount of id's appearing in each select - e.g. id=1 might have rows, id=2 might only have 1 row. that's the reason I have to include the ID in each select i do. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-04-27 : 12:25:35
|
If you are trying to exclude where PageName is not like "something" you should be using all ANDs and no ORs..(logic issue)DECLARE @Table TABLE (id VARCHAR(10), pageName VARCHAR(50))INSERT @TableSELECT '1', 'Cats - How to grow your own'UNION ALL SELECT '1', 'Dogs - Why do they bark'UNION ALL SELECT '1', 'Fish - What to feed them'UNION ALL SELECT '2', 'Cats - Why do they purr'UNION ALL SELECT '2', 'Fish - Fins and gills'select * from @table whereid like '1' and pagename not like 'cats%' and pagename not like 'dogs%'-- Resultsid pageName---------- --------------------------------------------------1 Fish - What to feed them |
 |
|
|
|