问题
I have a TransactionDetails
table with TransactionId
, TransactionDetail
columns, those are used to track all the transactions within the application.
The sample records of the TransactionDetail are:
[Blah][Log] 20 Records Inserted
[Blah][Exception] Fails on INSERT INTO
[Blah][Error] Authentication Fails Logged
[Blah][Warning] Null value is eliminated by an aggregate
[Blah][Log] 10 Records Deleted
I want to filter the TransactionDetail columns having the [Log] only (The Log, Logged keyword also exist in the other rows). The search query with []
consider as regular expression pattern. How can I skip that and get the required details only.
I have tried the \
escape character in front of the [
, ]
, but it doesn't return any result.
SELECT *
FROM TransactionDetails
WHERE TransactionDetail LIKE '%\[Log\]%'
Expected result:
[Blah][Log] 20 Records Inserted
[Blah][Log] 10 Records Deleted
SQL Fiddle: http://sqlfiddle.com/#!3/d69f3d/1
回答1:
Escape SQL wildcard characters by putting them in the group, i.e.:
SELECT *
FROM TransactionDetails
WHERE TransactionDetail LIKE '%[[]Log]%'
回答2:
As mentioned by jarlh in the comment the ESCAPE option also works:
SELECT *
FROM TransactionDetails
WHERE TransactionDetail LIKE '%#[Log#]%' ESCAPE '#'
Working Demo: http://sqlfiddle.com/#!3/d69f3d/37
Update: As per the reply by t-clausen.dk in this comment, the escape_character is not required for the ]
. So the code below also works.
SELECT *
FROM TransactionDetails
WHERE TransactionDetail LIKE '%#[Log]%' ESCAPE '#'
来源:https://stackoverflow.com/questions/35293531/search-keyword-with-like-keyword