Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

sql like - What does (backslash) mean in an SQL query?

I have the following query

SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'

will that result in answers that have any char+a++whatever?

is something like Pape valid as a result?

are Ca% or _a% valid answers maybe?

how does behave normally inside an SQL query??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

% is a wildcard character that matches zero or more characters in a LIKE clause. _ is a wildcard character that maches exactly one character in a LIKE clause.

is a special character known as an escape character that indicates that the character directly following it should be interpreted literally (useful for single quotes, wildcard characters, etc.).

For example:

SELECT txt1 FROM T1 WHERE txt1 LIKE '_a%'

will select records with txt1 values of 'xa1', 'xa taco', 'ya anything really', etc.

Now let's say you want to actually search for the percent sign. In order to do this you need a special character that indicates % should not be treated as a wildcard. For example:

SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'

will select records with txt1 values of 'ba%' (but nothing else).

Finally, a LIKE clause would typically contain a wildcard (otherwise you could just use = instead of LIKE). So you might see a query containing \%%. Here the first percent sign would be treated as a literal percent sign, but the second would be interpreted as a wildcard. For example:

SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%%'

will select records with txt1 values of 'da%something else', 'fa% taco', 'ma% bunch of tacos', etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...