March 5, 2013 at 2:46 am
hi,
I have searching for a query that returns a row if column value contains result of another query. Its like "select * from table1 where col contains(select col1 from table2 where <condition>)". Its just like using sub query in IN,but i want contains instead. Is there any way i can do this??
Thank you
March 5, 2013 at 2:48 am
winmansoft (3/5/2013)
Its just like using sub query in IN,but i want contains instead.
I aqm not sure what you are asking ? please clarify
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 5, 2013 at 2:51 am
Have a look at the query examples for the T-SQL operator EXISTS()
March 5, 2013 at 2:57 am
Its just like "SELECT * FROM entries WHERE work IN (select work from entries1 WHERE [date] BETWEEN '01-Jan-2012' and '20-Jan-2013')". But IN matches with exact values from result column of sub query. But i want to change matching to partial.
March 5, 2013 at 3:03 am
EXISTS might work. CONTAINS is used for querying full-text indexes.
But I think we need to understand what you need first. What do you mean by changing matched to partial?
Thanks
March 5, 2013 at 3:35 am
I will explain with example. I have a query "select work from entries1 WHERE [date] BETWEEN '01-Jan-2012' and '20-Jan-2013'" and lets say it will return result
training
checkout
surfing
develop
...
I have another table and which has Work column and it contains value 'checkout and play' in some row.I have to develop a query like "SELECT * FROM entries WHERE work IN (select work from entries1)" and i want row which contains 'checkout and play' in result. IN does not do it because it will not match 'checkout' with 'checkout and play' although 'checkout and play' contains 'checkout'.I want some other way to do it
March 5, 2013 at 3:39 am
winmansoft (3/5/2013)
I will explain with example. I have a query "select work from entries1 WHERE [date] BETWEEN '01-Jan-2012' and '20-Jan-2013'" and lets say it will return resulttraining
checkout
surfing
develop
...
I have another table and which has Work column and it contains value 'checkout and play' in some row.I have to develop a query like "SELECT * FROM entries WHERE work IN (select work from entries1)" and i want row which contains 'checkout and play' in result. IN does not do it because it will not match 'checkout' with 'checkout and play' although 'checkout and play' contains 'checkout'.I want some other way to do it
INNER JOIN the two tables using LIKE. If that doesn't work, you will need to split out the words in 'checkout and play'. It will work but it won't be quick.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 5, 2013 at 3:42 am
winmansoft (3/5/2013)
I have searching for a query that returns a row if column value contains result of another query. Its like "select * from table1 where col contains(select col1 from table2 where <condition>)". Its just like using sub query in IN,but i want contains instead. Is there any way i can do this??
Coming back to your first post , if you are using CONTAINS(fulltext search) then you can use FREETEXT there
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 5, 2013 at 3:48 am
Bhuvnesh (3/5/2013)
winmansoft (3/5/2013)
I have searching for a query that returns a row if column value contains result of another query. Its like "select * from table1 where col contains(select col1 from table2 where <condition>)". Its just like using sub query in IN,but i want contains instead. Is there any way i can do this??Coming back to your first post , if you are using CONTAINS(fulltext search) then you can use FREETEXT there
"select * from table1 where col contains(select col1 from table2 where <condition>)" wont work
March 5, 2013 at 3:50 am
ChrisM@Work (3/5/2013)
INNER JOIN the two tables using LIKE. If that doesn't work, you will need to split out the words in 'checkout and play'. It will work but it won't be quick.
Ca you write inner join query? and what do you mean by splitting the words?
March 5, 2013 at 3:53 am
winmansoft (3/5/2013)
Bhuvnesh (3/5/2013)
winmansoft (3/5/2013)
I have searching for a query that returns a row if column value contains result of another query. Its like "select * from table1 where col contains(select col1 from table2 where <condition>)". Its just like using sub query in IN,but i want contains instead. Is there any way i can do this??Coming back to your first post , if you are using CONTAINS(fulltext search) then you can use FREETEXT there
"select * from table1 where col contains(select col1 from table2 where <condition>)" wont work
See this link http://msdn.microsoft.com/en-us/library/ms176078(v=sql.105).aspx
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 5, 2013 at 3:56 am
winmansoft (3/5/2013)
ChrisM@Work (3/5/2013)
INNER JOIN the two tables using LIKE. If that doesn't work, you will need to split out the words in 'checkout and play'. It will work but it won't be quick.Ca you write inner join query? and what do you mean by splitting the words?
This is the simplest way to write it. If it generates dupes, then we'll change one side to a derived table. Can you use something a little more realistic than "entries" and "entries1", or is that what they are really called?
SELECT *
FROM entries e
INNER JOIN entries1 e1
ON e1.work LIKE e.work+'%'
AND e1.[date] BETWEEN '01-Jan-2012' and '20-Jan-2013'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 5, 2013 at 3:57 am
Bhuvnesh (3/5/2013)
See this link http://msdn.microsoft.com/en-us/library/ms176078(v=sql.105).aspx
Can use query instead of @SearchWord?? i dont think so
March 5, 2013 at 4:12 am
You need to implement Full-Text Indexing before freetext or contains will work.
I think Chris's solution is the way to go unless you find it's not what you need or that the performance isn't good enough, which could be quite likely with joins using like.
Depends on your data, but you might need to do INNER JOIN entries1 e1 ON e1.work LIKE '%'+e.work+'%'
But that will make performance even worse!
March 5, 2013 at 4:14 am
Gazareth (3/5/2013)
You need to implement Full-Text Indexing before freetext or contains will work.I think Chris's solution is the way to go unless you find it's not what you need or that the performance isn't good enough, which could be quite likely with joins using like.
Depends on your data, but you might need to do INNER JOIN entries1 e1 ON e1.work LIKE '%'+e.work+'%'
But that will make performance even worse!
+1
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply