0

I am trying to set up a query to search a column for a specific grouping of characters, for example "EPC". For the most part, the initial three characters in the string would contain "EPC", but for some of the descriptions, it might look like "XXX-EPC", and the amount/type of characters before "EPC" can vary.

Is there a way to limit the number of characters a LIKE query can search within a description, so for example search for "EPC" in the first 8 characters of a description, and if "EPC" exists grouped together anywhere in those first 8 characters, returns that data entry?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Emburgh
  • 3
  • 1
  • 1
    The two answers that are currently posted both look like they will yield the results that you're looking for. However I wanted to comment that I can't think of a reasonable solution to this that is [sargable](http://stackoverflow.com/questions/799584/what-makes-a-sql-statement-sargable). And as such, performance for these queries will be poor. (Just something to keep in mind.) – DeadZone Mar 12 '15 at 15:25

2 Answers2

1

You can't do what you want just with LIKE, but you could use LEFT to get the first 8 charcaters then apply LIKE to that.

SELECT* FROM EarnedHours WHERE LEFT(MatlDesc, 8) LIKE '*EPC*'
Rhys Jones
  • 5,348
  • 1
  • 23
  • 44
  • Here's the SQL I currently have to return all entries with "EPC" at the beginning of the material description. SELECT* FROM EarnedHours WHERE MatlDesc LIKE 'EPC*'; How would I include LEFT to this to search the first 8 characters? And would it return only the first 8 characters in from MatlDesc or would it return the entire description? – Emburgh Mar 12 '15 at 14:47
0

You could make use of Left and InStr maybe?

SELECT
    yourFields
FROM
    yourTable
WHERE
    InStr(Left(theFieldName, 8), "EPC") <> 0
PaulFrancis
  • 5,748
  • 1
  • 19
  • 36