February 5, 2010 at 8:26 am
Hi,
i have two tables like
table 1:
Itemno | Name |
123 WASH Detergent
table 2:
itemno | Decription|
Can you tell me how to write a query where it must choose a particular word in the table1 and bring the all rows from second table2 exactly from the description column the word name which is specified.
for example when i give '%ASH%' in the query it is giving me 'WASH Detergent'
its not giving me exact word which i want as it is giving me duplicate values like that.
so can u help me how to get an exact word without getting duplicate values.
Thanks,
S
February 5, 2010 at 9:12 am
i think this is what you are after;
the "description" field must be like the NAME, and the NAME must be like your search parameter:
so the "description" must be like '%WASH Detergent%' becuase it was foudn witht he"%ASH%' search parameter:
select * from Table2
inner join Table 1 on TABLE2.Description LIKE '%' + TABLE1.NAME +'%'
WHERE TABLE1.NAME LIKE '%ASH%'
Lowell
February 5, 2010 at 9:43 am
If you want the word, then try putting a space before/after the wildcards.
To handle the situation when this word would be at the beginning/ending of the column, add a beginning/ending space on the column.
i.e. (to borrow from Lowell)
select * from Table2
inner join Table 1 on TABLE2.Description LIKE '%' + TABLE1.NAME +'%'
WHERE ' ' + TABLE1.NAME + ' ' LIKE ' %ASH% '
By having a wildcard at the beginning of the like expression, you already aren't using an index, so adding the spaces around the field won't hurt performance any worse than what is already being done.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply