Home Explore Blog CI



postgresql

66th chunk of `doc/src/sgml/func.sgml`
846fc9eb90821cbeea31b325e5c8920506c64b7cc02c15f60000000100000fa3
 <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
</synopsis>

    <para>
     The <function>LIKE</function> expression returns true if the
     <replaceable>string</replaceable> matches the supplied
     <replaceable>pattern</replaceable>.  (As
     expected, the <function>NOT LIKE</function> expression returns
     false if <function>LIKE</function> returns true, and vice versa.
     An equivalent expression is
     <literal>NOT (<replaceable>string</replaceable> LIKE
      <replaceable>pattern</replaceable>)</literal>.)
    </para>

    <para>
     If <replaceable>pattern</replaceable> does not contain percent
     signs or underscores, then the pattern only represents the string
     itself; in that case <function>LIKE</function> acts like the
     equals operator.  An underscore (<literal>_</literal>) in
     <replaceable>pattern</replaceable> stands for (matches) any single
     character; a percent sign (<literal>%</literal>) matches any sequence
     of zero or more characters.
    </para>

   <para>
    Some examples:
<programlisting>
'abc' LIKE 'abc'    <lineannotation>true</lineannotation>
'abc' LIKE 'a%'     <lineannotation>true</lineannotation>
'abc' LIKE '_b_'    <lineannotation>true</lineannotation>
'abc' LIKE 'c'      <lineannotation>false</lineannotation>
</programlisting>
   </para>

   <para>
    <function>LIKE</function> pattern matching supports nondeterministic
    collations (see <xref linkend="collation-nondeterministic"/>), such as
    case-insensitive collations or collations that, say, ignore punctuation.
    So with a case-insensitive collation, one could have:
<programlisting>
'AbC' LIKE 'abc' COLLATE case_insensitive    <lineannotation>true</lineannotation>
'AbC' LIKE 'a%' COLLATE case_insensitive     <lineannotation>true</lineannotation>
</programlisting>
    With collations that ignore certain characters or in general that consider
    strings of different lengths equal, the semantics can become a bit more
    complicated.  Consider these examples:
<programlisting>
'.foo.' LIKE 'foo' COLLATE ign_punct    <lineannotation>true</lineannotation>
'.foo.' LIKE 'f_o' COLLATE ign_punct    <lineannotation>true</lineannotation>
'.foo.' LIKE '_oo' COLLATE ign_punct    <lineannotation>false</lineannotation>
</programlisting>
    The way the matching works is that the pattern is partitioned into
    sequences of wildcards and non-wildcard strings (wildcards being
    <literal>_</literal> and <literal>%</literal>).  For example, the pattern
    <literal>f_o</literal> is partitioned into <literal>f, _, o</literal>, the
    pattern <literal>_oo</literal> is partitioned into <literal>_,
    oo</literal>.  The input string matches the pattern if it can be
    partitioned in such a way that the wildcards match one character or any
    number of characters respectively and the non-wildcard partitions are
    equal under the applicable collation.  So for example, <literal>'.foo.'
    LIKE 'f_o' COLLATE ign_punct</literal> is true because one can partition
    <literal>.foo.</literal> into <literal>.f, o, o.</literal>, and then
    <literal>'.f' = 'f' COLLATE ign_punct</literal>, <literal>'o'</literal>
    matches the <literal>_</literal> wildcard, and <literal>'o.' = 'o' COLLATE
    ign_punct</literal>.  But <literal>'.foo.' LIKE '_oo' COLLATE
    ign_punct</literal> is false because <literal>.foo.</literal> cannot be
    partitioned in a way that the first character is any character and the
    rest of the string compares equal to <literal>oo</literal>.  (Note that
    the single-character wildcard always matches exactly one character,
    independent of the collation.  So in this example, the
    <literal>_</literal> would match <literal>.</literal>, but then the rest
    of the input string won't match the rest of the pattern.)
   </para>

   <para>
    <function>LIKE</function> pattern matching always covers the entire
    string.  Therefore, if it's desired

Title: LIKE Expression in PostgreSQL: Syntax and Examples
Summary
This section explains the LIKE expression in PostgreSQL, used for pattern matching. It details the syntax, including the optional ESCAPE clause. The function returns true if the string matches the pattern. It explains how underscores (_) match any single character and percent signs (%) match any sequence of zero or more characters. Several examples show different scenarios, including the behavior with nondeterministic collations like case-insensitive or punctuation-ignoring collations.