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 Để tìm kiếm Hàng Với Nhân vật đặc biệt trong 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. Trong khi xử lý sự cố một vấn đề lập trình hôm nay tôi nhận thấy rằng bạn không thể sử dụng một tìm kiếm LIKE cho cột có chứa chuỗi ký tự đặc biệt như% hay _ mà không cần sử dụng một cú pháp đặc biệt. Figuring out the problem took only a few minutes, but remembering syntax is always easier if you write about it. Figuring ra vấn đề này chỉ mất một vài phút, nhưng nhớ cú pháp luôn luôn dễ dàng hơn nếu bạn viết về nó.

So yes, this post is solely for my benefit. Vì vậy, có, bài đăng này là chỉ duy nhất cho lợi ích của tôi. Hopefully it will help somebody else too. Hy vọng rằng nó sẽ giúp người khác quá.

Let's say you want to find any fields that contain the text “100%”, so you put together this query: Hãy nói rằng bạn muốn tìm thấy bất kỳ lĩnh vực có chứa chữ "100%", vì vậy bạn để cùng nhau truy vấn này:

SELECT * FROM tablename WHERE fieldname LIKE '%100%%' SELECT * FROM tablename% WHERE 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%”. Thay vì những gì bạn muốn, bạn sẽ nhận được tất cả các hàng có chứa "100" cũng như các hàng có chứa "100%".

The problem here is that SQL Server uses the percent sign, underscore, and square brackets as special characters. Vấn đề ở đây là SQL Server sử dụng các dấu hiệu cho ngân hàng, gạch dưới, và dấu ngoặc vuông như là ký tự đặc biệt. You simply can't use them as a plain character in a LIKE query without escaping them. Bạn chỉ đơn giản là không thể sử dụng chúng như là một nhân vật đồng bằng trong một truy vấn NHỮNG mà không thoát chúng.

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. Bạn có thể bao quanh hoặc% _ với dấu ngoặc vuông để nói SQL Server là nhân vật bên trong là một nhân vật thường xuyên.

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

T-SQL ESCAPE Syntax T-SQL ESCAPE Cú pháp

Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape. Ngoài ra, bạn có thể gắn thêm các nhà điều hành ESCAPE vào truy vấn của bạn, và thêm một ký tự \ trước khi giá trị bạn muốn thoát ra khỏi.

SELECT * FROM tablename WHERE fieldname LIKE '%100\%%' ESCAPE '\' SELECT * FROM tablename% WHERE 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. Các ESCAPE '\' một phần trong truy vấn SQL cho động cơ để giải thích các ký tự sau \ là một nhân vật đen thay vì như là một ký tự đại diện.

Personally I find the second method easier to deal with, and you can use it to escape a square bracket as well. Cá nhân tôi thấy phương pháp thứ hai dễ dàng hơn để đối phó với, và bạn có thể sử dụng nó để thoát khỏi một khung vuông là tốt.

This article was originally written on 11/8/07 Tagged with: Bài viết này ban đầu được viết ngày 11/8/07 Tagged với: Geek Stuff Geek Stuff

Daily Email Updates Email cập nhật hàng ngày

You can get our how-to articles in your inbox each day for free. Bạn có thể làm thế nào chúng tôi vào các bài viết trong hộp thư của bạn mỗi ngày miễn phí. Just enter your name and email below: Chỉ cần nhập tên và email của bạn dưới đây:


Name: Tên:
Email: Email:

Comments (2) Comment (2)

  1. Whatever-ishere Dù-ishere

    thanks for the GREAT post! nhờ những bài GREAT! Very useful… Rất hữu ích ...

  2. Ashutosh Mahamia Ashutosh Mahamia

    This write up was extremely beneficial for me . Điều này đã được viết lên rất có lợi cho tôi. Thanks for writing it. Cảm ơn bạn đã viết nó. Keep it up. Keep it up.


Leave a Comment Để lại một Thảo luận




Leave your Để lại của bạn friendly thân thiện comment here. bình luận ở đây.

If you have a computer help question, Nếu bạn có một câu hỏi giúp máy tính, click here to leave it on the forums bấm vào đây để lại nó trên các diễn đàn instead. thay vì.

Note: Your comment may not show up immediately on the site. Lưu ý: Ý kiến của bạn có thể không hiển thị ngay lập tức trên trang web.

Our Friends Bạn bè của chúng tôi
Getting Started Bắt đầu


About How-To Geek Giới How-To Geek
What Is That Process? Quá trình đó là gì?
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. Bản quyền © 2006-2009 HowToGeek.com. All Rights Reserved. Tất cả các quyền.