Search For Rows With Special Characters in SQL Server Ieškoti eilutės su specialių ženklų 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. Nors trikčių programavimo problema šiandien aš pastebėjau, kad jūs negalite naudoti PANAŠŪS paieška styginių stulpelių, kurių sudėtyje yra specialių simbolių, pavyzdžiui% _ arba nenaudojant specialią sintaksę. Figuring out the problem took only a few minutes, but remembering syntax is always easier if you write about it. Suprasti, problema buvo tik keletą minučių, tačiau nepamiršti sintaksė yra visada lengviau, jei rašote apie tai.
So yes, this post is solely for my benefit. Taip taip, šis laiškas yra tik mano naudai. Hopefully it will help somebody else too. Tikimės, kad tai padės kuriam nors kitam taip pat.
Let's say you want to find any fields that contain the text “100%”, so you put together this query: Tarkime, jūs norite rasti visus laukus, kuriuose yra tekstas "100%", todėl jums sujungti šią užklausą:
SELECT * FROM tablename WHERE fieldname LIKE '%100%%' SELECT * FROM tablename KUR 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%”. Vietoj to, ką norėjo, gausite visas eilutes, kuriose yra "100", taip pat tas eilutes, kurios yra "100%".
The problem here is that SQL Server uses the percent sign, underscore, and square brackets as special characters. Problema čia yra ta, kad SQL Server naudoja procento ženklas, pabrėžiantis, ir skliaustus kaip specialių simbolių. You simply can't use them as a plain character in a LIKE query without escaping them. Jūs tiesiog negali jomis naudotis, kaip paprastas simbolis PANAŠŪS užklausą pabėgti be jų.
Square Bracket Escape Square Bracket Escape
You can surround the % or _ with square brackets to tell SQL Server that the character inside is a regular character. Galite supantys% _ arba su skliaustus sužinoti SQL Server, pobūdį viduje yra nuolatinis pobūdis.
SELECT * FROM tablename WHERE fieldname LIKE '%100[%]%' SELECT * FROM tablename KUR fieldName LIKE '% 100 [%]%'
T-SQL ESCAPE Syntax T-SQL sintaksės ESCAPE
Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape. Arba galite pridėti ESCAPE operatorius į savo užklausą ir pridėti \ savybių prieš vertė norite pabėgti.
SELECT * FROM tablename WHERE fieldname LIKE '%100\%%' ESCAPE '\' SELECT * FROM tablename KUR 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 '\' dalis užklausą pasakoja devspaces aiškinti pobūdis po \ taip pažodžiui pobūdžio, o ne kaip pakaitos.
Personally I find the second method easier to deal with, and you can use it to escape a square bracket as well. Asmeniškai manau, antrasis metodas lengviau spręsti, ir jūs galite jį naudoti pabėgti kvadratinių grupėje taip pat.

Daily Email Updates Dienos paštas Atnaujinimai
You can get our how-to articles in your inbox each day for free. Galite gauti mūsų kaip prie straipsnių į Jūsų pašto dėžutę kasdien nemokamai. Just enter your name and email below: Tiesiog įveskite vardą ir elektroninio pašto adresą žemiau:



thanks for the GREAT post! Thanks for the great post! Very useful… Labai naudinga ...
This write up was extremely beneficial for me . Šis parašyti buvo labai naudingas man. Thanks for writing it. Ačiū už tai raštu. Keep it up. Keep it up.