March 11, 2016 at 9:59 am
I need to find all items that end with B and S.
Not sure what I am doing wrong, if I do just one letter then it works fine. Its when I try to do both in the same query that it messes up. What am I doing wrong?
Here is my code..
SELECT * FROM ITEMMAST
WHERE ITEM LIKE '%S' and '%B'
March 11, 2016 at 10:06 am
Try
WHERE Item LIKE '%S' OR Item LIKE '%B'
-- Itzik Ben-Gan 2001
March 11, 2016 at 1:06 pm
yes that worked! thanks...
1. Why would OR work? OR sounds like I need only either S or B, but in reality I need BOTH. Strange...
2. Is there a way I can do a ORDER BY , to sort the data... ORDER BY the S or B so they are in order?
March 11, 2016 at 1:43 pm
wallywizard (3/11/2016)
yes that worked! thanks...1. Why would OR work? OR sounds like I need only either S or B, but in reality I need BOTH. Strange...
Could you provide at least one example when ONE SINGLE item ends with 'S' AND 'B' at the same time?
wallywizard (3/11/2016)
2. Is there a way I can do a ORDER BY , to sort the data... ORDER BY the S or B so they are in order?
Is this what you need?
ORDER BY RIGHT(item, 1)
March 11, 2016 at 2:13 pm
sorry let me rephrase....I dont have an item that ends both S and B at the same time.
There are tons of items, I am only looking to pull items that end with B (4857B) and items that end with S (1240S)
That OR function worked great, but what I was saying is that OR sounds like it wants to pull only one of these.. I assumed AND would work because I want both B AND S.
I tried your order by but doesnt seem to sort my data. (pic attached)
March 11, 2016 at 2:27 pm
wallywizard (3/11/2016)
I tried your order by but doesnt seem to sort my data. (pic attached)
Try harder.
March 11, 2016 at 2:56 pm
wallywizard (3/11/2016)
sorry let me rephrase....I dont have an item that ends both S and B at the same time.There are tons of items, I am only looking to pull items that end with B (4857B) and items that end with S (1240S)
That OR function worked great, but what I was saying is that OR sounds like it wants to pull only one of these.. I assumed AND would work because I want both B AND S.
I tried your order by but doesnt seem to sort my data. (pic attached)
OR does only want to pull one of these - which is what you want. Each individual row is considered according to the entire WHERE clause - that is, for a row to appear in the results, everything in the WHERE clause must be true for that row.
When you use OR what you're telling SQL is that each row must have an item that ends in "B" or that ends in "S" - you don't care which one, it has to be one of those two.
When you use AND what you're telling SQL is that each row must have an item that ends in "B" AND that ends in "S" - which is logically impossible, so you'll never get results.
March 11, 2016 at 8:06 pm
You may already know this, but having that leading wildcard in the search will prevent SQL Server from using an index on that column, so your query is going to run slowly. One alternative to this is to create a computed persisted column to store the right 1 character, then put a nonclustered index on that column. Then you won't have to search against the item number, but rather by the column that holds the single character. The column would also work for your sort.
You could then continue to use the OR, or you can use an IN clause like this:
SELECT *
FROM ITEMMAST
WHERE LastChar IN ('B', 'S')
ORDER BY LastChar;
March 12, 2016 at 10:48 am
Ed Wagner (3/11/2016)
You may already know this, but having that leading wildcard in the search will prevent SQL Server from using an index on that column, so your query is going to run slowly. One alternative to this is to create a computed persisted column to store the right 1 character, then put a nonclustered index on that column. Then you won't have to search against the item number, but rather by the column that holds the single character. The column would also work for your sort.You could then continue to use the OR, or you can use an IN clause like this:
SELECT *
FROM ITEMMAST
WHERE LastChar IN ('B', 'S')
ORDER BY LastChar;
+1
You've gotta know what's coming next... someone will want the value prior to the last character. An additional computed, persisted, indexed column for that should probably be a "just do it so when they ask, it's already done" moment.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 14, 2016 at 6:11 am
Alan.B (3/11/2016)
TryWHERE Item LIKE '%S' OR Item LIKE '%B'
WHERE Item like '%[S,B]'
March 14, 2016 at 6:24 am
planetmatt (3/14/2016)
Alan.B (3/11/2016)
TryWHERE Item LIKE '%S' OR Item LIKE '%B'
WHERE Item like '%[S,B]'
That leading wildcard in the search will force a scan of the column, so the query will run slowly. An index, if present, won't be used. The persisted computed column is a better approach.
Edit: For regular expressions, I think the pipe (|) is the character for an OR. I don't use them much because they're also slow.
March 14, 2016 at 7:06 am
Ed Wagner (3/14/2016)
planetmatt (3/14/2016)
Alan.B (3/11/2016)
TryWHERE Item LIKE '%S' OR Item LIKE '%B'
WHERE Item like '%[S,B]'
That leading wildcard in the search will force a scan of the column, so the query will run slowly. An index, if present, won't be used. The persisted computed column is a better approach.
Edit: For regular expressions, I think the pipe (|) is the character for an OR. I don't use them much because they're also slow.
It should actually be:
WHERE Item LIKE '%[SB]'
On the other side, I agree with the use of a computed column. I just disagree on which should it be. The Item column should be splitted into 2 different columns if each part represents a different attribute, and then it should be joined for display.
For the ORDER, the column might be a char with trailing spaces. RIGHT won't eliminate trailing spaces on char columns, only on varchar.
CREATE TABLE #Test( Item char(10), Item2 varchar(10));
INSERT INTO #Test VALUES('12345B','12345B');
SELECT RIGHT(Item,1), RIGHT(RTRIM(Item),1),
RIGHT(Item2,1), RIGHT(RTRIM(Item2),1)
FROM #Test;
GO
DROP TABLE #Test;
March 14, 2016 at 7:33 am
cphite (3/11/2016)
wallywizard (3/11/2016)
sorry let me rephrase....I dont have an item that ends both S and B at the same time.There are tons of items, I am only looking to pull items that end with B (4857B) and items that end with S (1240S)
That OR function worked great, but what I was saying is that OR sounds like it wants to pull only one of these.. I assumed AND would work because I want both B AND S.
I tried your order by but doesnt seem to sort my data. (pic attached)
OR does only want to pull one of these - which is what you want. Each individual row is considered according to the entire WHERE clause - that is, for a row to appear in the results, everything in the WHERE clause must be true for that row.
When you use OR what you're telling SQL is that each row must have an item that ends in "B" or that ends in "S" - you don't care which one, it has to be one of those two.
When you use AND what you're telling SQL is that each row must have an item that ends in "B" AND that ends in "S" - which is logically impossible, so you'll never get results.
Great tip, thanks for that, helps break it down better. And thanks to everyone, Im a newbie with sql and your posts help alot. Building my knowledge one post at a time..... and knowing is half the battle...GI Jooeeeeeeeeeeee (not sure if anyone will get that last part 😉 )
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply