Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

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. באופן אישי אני מוצא את השיטה השניה יותר קל להתמודד עם, ואתה יכול להשתמש בו כדי לברוח סוגר ריבוע וכן.

This article was originally written on 11/8/07 Tagged with: מאמר זה נכתב במקור על 11/8/07 שתייגת: Geek Stuff Geek Stuff

Daily Email Updates שערי עדכונים בדוא"ל

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


Name: שם:
Email: דואר אלקטרוני:

Comments (2) תגובות (2)

  1. Whatever-ishere מה-ishere

    thanks for the GREAT post! תודה על הפוסט GREAT! Very useful… מאוד שימושי ...

  2. Ashutosh Mahamia Ashutosh Mahamia

    This write up was extremely beneficial for me . לכתוב את זה היה מאוד מועיל לי. Thanks for writing it. תודה שכתבת את זה. Keep it up. Keep it up.


Leave a Comment השאירו תגובה




Leave your השאירו שלך friendly ידידותי comment here. תגובה כאן.

If you have a computer help question, אם יש לך מחשב לעזור השאלה, click here to leave it on the forums לחץ כאן כדי להשאיר אותו על פורומים instead. במקום.

Note: Your comment may not show up immediately on the site. הערה: ההערה שלך לא תופיע מיד באתר.

Our Friends חברים שלנו
Getting Started התחלת העבודה


About How-To Geek אודות How-To Geek
What Is That Process? מה זה תהליך?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe Ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. זכויות יוצרים © 2006-2009 HowToGeek.com. All Rights Reserved. כל הזכויות שמורות.