-1

I have a text field that contains a percentage sign. I need 3 chars from the left of that percentage sign...but since the percentage sign is a wildcard I'm having some issues.

charindex(field,'%') from...

That is how I'd normally go about this as part of a substring function, but since % is a wildcard, the charindex returns 0 for any non null record. Any idea's on how I specify this to the char '%' instead of the % wildcard?

Tried to search this one on the forums, but just ended up sifting through unrelated questions.

Alex
  • 34,899
  • 5
  • 77
  • 90
Twelfth
  • 7,070
  • 3
  • 26
  • 34
  • So many duplicates: http://stackoverflow.com/search?q=like+escape+sql – Mitch Nov 04 '13 at 23:29
  • possible duplicate of [Escape a string in SQL Server so that it is safe to use in LIKE expression](http://stackoverflow.com/questions/258757/escape-a-string-in-sql-server-so-that-it-is-safe-to-use-in-like-expression) – Mitch Nov 04 '13 at 23:30
  • Figured there was a few duplicates...couldn't remember the term 'escape a string' to search for it for the life of me. – Twelfth Nov 05 '13 at 00:23
  • 1
    @Twelfth . . . To the best of my understanding, `'%'` is a wildcard for `like`. `charindex()` doesn't accept wildcards. You can see a test here (http://www.sqlfiddle.com/#!6/d41d8/11183). – Gordon Linoff Nov 05 '13 at 01:26

2 Answers2

3

Escape the percent sign %:

charindex(field,'[%]') from...
Alex
  • 34,899
  • 5
  • 77
  • 90
0

Try charindex('%', field) from... instead