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
 How to implement search?

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2010-09-22 : 07:34:16
Hello,

I have an entity named Book with 3 fields: Title, Description and Notes.

And I have two words: "Camões" and "Manuscript".

I would like to get all records that contains one or both words in any of the fields described above.

How can I do this?

And can I do the following?

1 - Word "Camões" would be the same as "Camoes";

2 - Order the results by the number of occurrences.

First books where the two words were found.

Second books were only one word was found.

Is this possible?

Thank You,

Miguel

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-22 : 09:00:07
quote:
Originally posted by shapper

Hello,

I have an entity named Book with 3 fields: Title, Description and Notes.

And I have two words: "Camões" and "Manuscript".

I would like to get all records that contains one or both words in any of the fields described above.

How can I do this?





SELECT * FROM book
WHERE Title LIKE '%Camões%' OR Title LIKE '%Manuscript%' OR description LIKE '%Camões%' OR description LIKE '%Manuscript%'
OR notes LIKE '%Camões%' OR notes LIKE '%Manuscript%'
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-22 : 09:26:13
quote:
Originally posted by shapper

Hello,

I have an entity named Book with 3 fields: Title, Description and Notes.

And I have two words: "Camões" and "Manuscript".

I would like to get all records that contains one or both words in any of the fields described above.


And can I do the following?

2 - Order the results by the number of occurrences.

First books where the two words were found.

Second books were only one word was found.

Is this possible?

Thank You,

Miguel



Yes its possible and here is the query:
* Word "Camões" would be the same as "Camoes"; NOT HANDLED



CREATE TABLE #temp
(
id INT IDENTITY(1,1),
Title VARCHAR(200),
Description VARCHAR(500),
Notes VARCHAR(150)
)

-- First books where the two words were found.
INSERT INTO #temp
SELECT * FROM book
WHERE -- case when both Camões & Manuscript present in one of the 3 columns in any order
Title LIKE '%Camões%Manuscript%' OR notes LIKE '%Camões%Manuscript%' OR description LIKE '%Camões%Manuscript%'
OR Title LIKE '%Manuscript%Camões%' OR notes LIKE '%Manuscript%Camões%' OR description LIKE '%Manuscript%Camões%'

--case when Camões Or Manuscript present in one of the 3 columns and the second search text in some other(2 out of 3) column
OR Title LIKE '%Camões%' AND (description LIKE '%Manuscript%' OR notes LIKE '%Manuscript%')
OR description LIKE '%Camões%' AND (Title LIKE '%Manuscript%' OR notes LIKE '%Manuscript%')
OR notes LIKE '%Camões%' AND (Title LIKE '%Manuscript%' AND description LIKE '%Manuscript%')

OR Title LIKE '%Manuscript%' AND (description LIKE '%Manuscript%' OR notes LIKE '%Camões%')
OR description LIKE '%Manuscript%' AND (Title LIKE '%Camões%' OR notes LIKE '%Camões%')
OR notes LIKE '%Manuscript%' AND (Title LIKE '%Camões%' AND description LIKE '%Camões%')


-- Second books were only one word was found.
INSERT INTO #temp
SELECT * FROM book
WHERE
(Title LIKE '%Camões%' AND Title NOT LIKE '%Manuscript%' AND (description NOT LIKE '%Manuscript%' AND notes NOT LIKE '%Manuscript%')) OR
(Title LIKE '%Manuscript%' AND Title NOT LIKE '%Camões%' AND (description NOT LIKE '%Camões%' AND notes NOT LIKE '%Camões%')) OR

(description LIKE '%Camões%' AND description NOT LIKE '%Manuscript%' AND (Title NOT LIKE '%Manuscript%' AND notes NOT LIKE '%Manuscript%')) OR
(description LIKE '%Manuscript%' AND description NOT LIKE '%Camões%' AND (Title NOT LIKE '%Camões%' AND notes NOT LIKE '%Camões%')) OR

(notes LIKE '%Camões%' AND notes NOT LIKE '%Manuscript%' AND (Title NOT LIKE '%Manuscript%' AND description NOT LIKE '%Manuscript%')) OR
(notes LIKE '%Manuscript%' AND notes NOT LIKE '%Camões%' AND (Title NOT LIKE '%Camões%' AND description NOT LIKE '%Camões%'))

SELECT * FROM #temp order by id ASC -- Order the results by the number of occurrences.
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2010-09-22 : 10:25:38
In this case I gave two words but can be more.
It was just an example.
Shouldn't I use Full Text Search?

Thank you,
Miguel
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-09-22 : 10:37:11
quote:
Originally posted by shapper

In this case I gave two words but can be more.
It was just an example.
Shouldn't I use Full Text Search?

Thank you,
Miguel



would be better
you can use the logic to order them by the two conditions you have given
2 - Order the results by the number of occurrences.

First books where the two words were found.

Second books were only one word was found.

with the help of
CREATE TABLE #temp
(
id INT IDENTITY(1,1),
Title VARCHAR(200),
Description VARCHAR(500),
Notes VARCHAR(150)
)

and
SELECT * FROM #temp order by id ASC
Go to Top of Page
   

- Advertisement -