0

Trying to build a SQL statement in which I can filter reports by a narrative field containing the word bike. Need to include those reports containing bike and bikeway, but omit those only containing bikeway.

Seems like I need to filter by bike followed by a space, but how to get it to recognize that space as a character? New at this; not familiar enough to get this on my own!

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • 1
    Space is not enough: presumably you also want to match `bike.`, `bike,`, `bikes`, etc. So you may want a regular expression search or perhaps a fuzzy match full text search. Both those things are not covered by the ANSI standard, so the solution will be heavily dependent on the functionality offered by your particular database flavour. – APC Jan 01 '13 at 10:06
  • What flavour of SQL are you using? SQL server, oracle, IBM? What does your table look like? What are you building the report in? – twoleggedhorse Jan 01 '13 at 10:17
  • Your questions should have more clarity.Please share your query and specify db. – sreejithsdev Jan 01 '13 at 10:27

1 Answers1

0

Need to include those reports containing bike and bikeway, but omit those only containing bikeway.

Use one word as an exact match, and the other using the LIKE clause, as in this example:

SELECT P.Id AS [Post Link]
FROM Posts P, PostTags Pt, Tags T
WHERE P.Id = Pt.PostId
AND T.Id = Pt.TagId
AND T.TagName = '##tag1##'
AND P.Tags like '<%##tag2##%>'
AND P.PostTypeId = 1
GROUP BY P.Id
HAVING (SELECT Count(Pt.TagId) FROM PostTags Pt WHERE Pt.PostId = P.Id) = 2

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265