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 Hľadanie Riadky sa špecifickými znakmi v 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. Kým problémov programovací problém dnes som si všimol, že nemôžete použiť LIKE hľadanie reťazca stĺpce obsahujúce špeciálne znaky ako _% a bez použitia špeciálna syntax. Figuring out the problem took only a few minutes, but remembering syntax is always easier if you write about it. Zistiť problém trvalo len pár minút, ale na pamäti syntax je vždy ľahšie, ak budete písať o tom.

So yes, this post is solely for my benefit. Takže áno, tento post je iba pre môj prospech. Hopefully it will help somebody else too. Dúfajme, že to pomôže aj niekomu inému.

Let's say you want to find any fields that contain the text “100%”, so you put together this query: Povedzme, že chcete nájsť polia, ktoré obsahujú text "100%", takže si dať dohromady tento dotaz:

SELECT * FROM tablename WHERE fieldname LIKE '%100%%' SELECT * FROM tablename KDE fieldname LIKE '% 100%%'

Instead of what you wanted, you'll get all the rows that contain “100″ as well as the rows that contain “100%”. Miesto toho, čo ste chceli, dostanete všetky riadky, ktoré obsahujú "100", ako aj riadky, ktoré obsahujú "100%".

The problem here is that SQL Server uses the percent sign, underscore, and square brackets as special characters. Problém je, že SQL Server používa znak percenta, podčiarkovník, a hranaté zátvorky za špeciálne znaky. You simply can't use them as a plain character in a LIKE query without escaping them. Jednoducho nemožno použiť ako holý znak v LIKE otázku bez úniku nich.

Square Bracket Escape Square Escape Bracket

You can surround the % or _ with square brackets to tell SQL Server that the character inside is a regular character. Môžete surround% alebo _ sa v hranatých zátvorkách povedať, SQL Server, že charakter vnútri je obyčajný znak.

SELECT * FROM tablename WHERE fieldname LIKE '%100[%]%' SELECT * FROM tablename KDE fieldname LIKE '% 100 [%]%'

T-SQL ESCAPE Syntax T-SQL ESCAPE Syntax

Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape. Prípadne môžete pripojiť ESCAPE operátor na Váš dotaz, a pridajte \ znaku, ako hodnota, ktorú chcete uniknúť.

SELECT * FROM tablename WHERE fieldname LIKE '%100\%%' ESCAPE '\' SELECT * FROM tablename KDE fieldname LIKE '% 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. ESCAPE '\' časť SQL dotaze hovorí motor interpretovať znak za \ ako doslovný osobnosť, nie ako zástupný.

Personally I find the second method easier to deal with, and you can use it to escape a square bracket as well. Osobne považujem druhý spôsob jednoduchšie riešenie, a môžete ich využiť na úteku hranatá zátvorka tiež.

This article was originally written on 11/8/07 Tagged with: Tento článok bol pôvodne napísaný na 11/8/07 Tagged s: Geek Stuff Geek Stuff

Daily Email Updates Denný Svářeč

You can get our how-to articles in your inbox each day for free. Môžete si naše jak-na články vo vašej schránky každý deň zadarmo. Just enter your name and email below: Stačí zadať svoje meno a e-mail nižšie:


Name: Meno:
Email: E-mail:

Comments (2) Komentáre (2)

  1. Whatever-ishere Či-ishere

    thanks for the GREAT post! Vďaka za skvelý post! Very useful… Veľmi užitočné ...

  2. Ashutosh Mahamia Ashutosh Mahamia

    This write up was extremely beneficial for me . Tento zápis sa bol veľmi prínosné pre mňa. Thanks for writing it. Vďaka za písanie. Keep it up. Keep it up.


Our Friends Naši priatelia
Getting Started Začíname


About How-To Geek O tom, ako-To Geek
What Is That Process? Čo je to za proces?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe Dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.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. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. All Rights Reserved.