Search For Rows With Special Characters in SQL Server חיפוש עבור שורות עם תווים מיוחדים של SQL Server
While troubleshooting a programming problem today I noticed that you can't use a LIKE search for string columns containing special characters like % or _ without using a special syntax. בעת פתרון בעיות תכנות בעיה היום שמתי לב שאתה לא יכול להשתמש עבור עמודות חיפוש כמו מחרוזת המכילה תווים מיוחדים כמו% או _ ללא שימוש בתחביר מיוחד. Figuring out the problem took only a few minutes, but remembering syntax is always easier if you write about it. להבין את הבעיה לקח רק כמה דקות, אבל התחביר לזכור תמיד קל יותר אם תכתוב על זה.
So yes, this post is solely for my benefit. אז כן, הודעה זו היא אך ורק למעני. Hopefully it will help somebody else too. בתקווה שזה יעזור למישהו אחר מדי.
Let's say you want to find any fields that contain the text “100%”, so you put together this query: בוא נגיד אתה רוצה למצוא את כל השדות המכילים את הטקסט "100%", כך שתוכל להרכיב את השאילתה:
SELECT * FROM tablename WHERE fieldname LIKE '%100%%' SELECT * FROM tablename% כמו איפה fieldname '% 100%'
Instead of what you wanted, you'll get all the rows that contain “100″ as well as the rows that contain “100%”. במקום מה שאתה רוצה, תקבל את כל השורות המכילות "100", וכן את השורות המכילות "100%".
The problem here is that SQL Server uses the percent sign, underscore, and square brackets as special characters. הבעיה כאן היא של SQL Server משתמש סימן האחוזים, תחתון, ו בסוגריים מרובעים כמו התווים המיוחדים. You simply can't use them as a plain character in a LIKE query without escaping them. אתה פשוט לא יכול להשתמש בהם בתור תו רגיל בשאילתה כזה בלי לברוח מהם.
Square Bracket Escape ריבוע Bracket בריחה
You can surround the % or _ with square brackets to tell SQL Server that the character inside is a regular character. אתה יכול להקיף את _% או עם סוגריים מרובעים לספר SQL Server כי האופי הפנימי הוא דמות קבועה.
SELECT * FROM tablename WHERE fieldname LIKE '%100[%]%' SELECT * FROM tablename% כמו איפה fieldname '100 [%]%'
T-SQL ESCAPE Syntax T-SQL ESCAPE תחביר
Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape. לחלופין, ניתן לצרף למפעיל ESCAPE אל השאילתה, ולהוסיף תו \ לפני בערך אתה רוצה לברוח.
SELECT * FROM tablename WHERE fieldname LIKE '%100\%%' ESCAPE '\' SELECT * FROM tablename% כמו איפה fieldname '100 \%%' ESCAPE '\'
The ESCAPE '\' part of the query tells the SQL engine to interpret the character after the \ as a literal character instead of as a wildcard. הבריחה '\' חלק השאילתה אומר את המנוע SQL לפרש את תו אחרי \ כתו מילולי במקום כתו כללי.
Personally I find the second method easier to deal with, and you can use it to escape a square bracket as well. באופן אישי אני מוצא את השיטה השניה יותר קל להתמודד עם, ואתה יכול להשתמש בו כדי לברוח סוגר ריבוע וכן.

Daily Email Updates שערי עדכונים בדוא"ל
You can get our how-to articles in your inbox each day for free. אתה יכול לקבל כמה שלנו למאמרים לתיבת הדואר שלך בכל יום בחינם. Just enter your name and email below: פשוט להזין את שם ואת הדוא"ל שלך להלן:



thanks for the GREAT post! תודה על הפוסט GREAT! Very useful… מאוד שימושי ...
This write up was extremely beneficial for me . לכתוב את זה היה מאוד מועיל לי. Thanks for writing it. תודה שכתבת את זה. Keep it up. Keep it up.