Subscribe to How-To Geek Mag-subscribe sa Paano-Upang Geek

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 Search For Mga hilera sa pamamagitan ng espesyal na mga karakter sa 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. Habang sa pag-troubleshoot ng isang programa sa ngayon problema ko napansin na hindi mo maaaring gamitin ang isang TULAD paghahanap para sa mga haligi ng string na naglalaman ng mga espesyal na character tulad ng% o _ na walang gamit ang isang espesyal na syntax. Figuring out the problem took only a few minutes, but remembering syntax is always easier if you write about it. Figuring ang problema kinuha lamang ng ilang minuto, ngunit remembering syntax ay palaging mas madali kung isulat sa inyo ang tungkol dito.

So yes, this post is solely for my benefit. Kaya oo, ang post na ito ay para lamang sa aking mga benepisyo. Hopefully it will help somebody else too. Sana ito ay makatulong sa isang tao pa rin.

Let's say you want to find any fields that contain the text “100%”, so you put together this query: Let's say gusto mong mahanap ang anumang larangan na naglalaman ng teksto ng "100%", kaya kayo magkasama sa query na ito:

SELECT * FROM tablename WHERE fieldname LIKE '%100%%' PUMILI * * * MULA tablename% SAAN fieldname TULAD '100%%'

Instead of what you wanted, you'll get all the rows that contain “100″ as well as the rows that contain “100%”. Sa halip na kung ano ang iyong nais, makakakuha ka ng lahat ng mga hanay na naglalaman ng "100" pati na rin ang mga hanay na naglalaman ng "100%".

The problem here is that SQL Server uses the percent sign, underscore, and square brackets as special characters. Ang problema dito ay na SQL Server gumagamit ng porsiyento mag-sign, salungguhit, at parisukat na bracket bilang espesyal na character. You simply can't use them as a plain character in a LIKE query without escaping them. Ikaw lamang ay hindi maaaring gamitin ang mga ito bilang isang malinaw na character sa isang TULAD tanong na walang escaping sa kanila.

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. Maaari mong palibutan ang% o _ na may parisukat na bracket na sabihin sa SQL Server na karakter sa loob ay isang regular na karakter.

SELECT * FROM tablename WHERE fieldname LIKE '%100[%]%' PUMILI * * * MULA tablename% SAAN fieldname TULAD '100 [%]%'

T-SQL ESCAPE Syntax T-SQL Syntax makatakas

Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape. Bilang kahalili, maaari mong maidagdag ang pagtakas sa operator papunta sa iyong query, at magdagdag ng isang \ character bago ang halaga na nais mong makatakas.

SELECT * FROM tablename WHERE fieldname LIKE '%100\%%' ESCAPE '\' PUMILI * * * MULA tablename% SAAN fieldname TULAD '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. Ang pagtakas '\' bahagi ng mga query sa nagsasabi ng SQL engine na bigyan ng kahulugan ang character pagkatapos ng \ bilang isang literal na character sa halip na bilang isang wildcard.

Personally I find the second method easier to deal with, and you can use it to escape a square bracket as well. Personal ko mahanap ang ikalawang paraan na mas madali ang makikitungo sa, at maaari mong gamitin ito upang makatakas sa isang parisukat na bracket rin.

This article was originally written on 11/8/07 Tagged with: Ang artikulong ito ay orihinal na isinulat sa 11/8/07 Tagged with: The Geek Blog Ang Geek Blog

Daily Email Updates Araw-araw na Updates Email

You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:


Name: Pangalan:
Email: Email:

Comments (2) Comments (2)

  1. Whatever-ishere Anuman-ishere

    thanks for the GREAT post! thanks para sa GREAT post! Very useful… Very kapaki-pakinabang ...

  2. Ashutosh Mahamia Ashutosh Mahamia

    This write up was extremely beneficial for me . Ito sumulat ng up ay lubhang kapaki-pakinabang para sa akin. Thanks for writing it. Salamat sa pagsulat nito. Keep it up. Panatilihin itong up.


Our Friends Ang aming mga Friends
Getting Started Pagsisimula


About How-To Geek Tungkol sa Paano-Upang Geek
What Is That Process? Ano ba ang Proseso Iyon?
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.