Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

pattern matching - SQL select where column begins with

I'm trying to find all the data in ColumnX where the Data begins with a .
Is like '\%' what I'm looking for? But the has to be at the beginning.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

The backslash can have special meaning in LIKE patterns. It's the default ESCAPE character in PostgreSQL or MySQL - but not in Oracle or SQL Server. (Can be modified with an added ESCAPE clause in subtly varying ways, follow link for your RDBMS.)

In addition to that, PostgreSQL used to interpret POSIX-style escapes in strings (a.k.a. "C escape syntax") before version 9.1 and MySQL still does in version 8.0). There you have to double to \ to get an ordinary backslash.

According to standard SQL, is not a meta character in strings. PostgreSQL eventually switched to standard behavior and introduced a special escape-string-syntax with E'' and the config settings standard_conforming_strings and escape_string_warning to still allow escaped string when needed.

Since Postgres 9.1 standard_conforming_strings = on by default. So you write:

... col LIKE '\%'    -- double once to get 1 literal backslash in LIKE pattern

Else you have to write:

... col LIKE E'\\%'  -- double twice to also disable special meaning as escape char

On top of that, also has special meaning in many client programs and programming languages. If strings get interpreted before they even reach the database engine, you may have to double the another time (or escape it in some other fashion). See this related question, for instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...