PostgreSQL equivalent to MySQL LOCATE()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zerogreen
    New Member
    • Sep 2007
    • 1

    PostgreSQL equivalent to MySQL LOCATE()

    Hi

    I am looking for an equivalent to the LOCATE function from MySQL.
    I have a query(PHP) that looks like this:

    Code:
    $query="SELECT * FROM table WHERE LOCATE('".$var."', field)> 0 ORDER BY LOCATE('".$var."', field) LIMIT 10";
    Any Ideas?
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Originally posted by zerogreen
    Hi

    I am looking for an equivalent to the LOCATE function from MySQL.
    I have a query(PHP) that looks like this:

    Code:
    $query="SELECT * FROM table WHERE LOCATE('".$var."', field)> 0 ORDER BY LOCATE('".$var."', field) LIMIT 10";
    Any Ideas?
    I do not know mysql, but does LOCATE work as regular expressions. If so u can use postgres LIKE (or ILIKE) statement.

    Comment

    • penguintamer

      #3
      This may be a little late, but if I got here from a Google search, other people will, too. So here's an answer:

      MySQL locate('substri ng','long string containing substring')
      is equivalent to
      PostgreSQL position('subst ring' IN 'long string containing substring')

      Comment

      • Adam Potter
        New Member
        • Dec 2010
        • 1

        #4
        I am having a similar problem.

        However I wish to use this form:
        LOCATE(substr,s tr,pos)

        Can someone offer me a way too do this in postgresql?
        (as 'position' does not contain the 'pos' function)

        Thanks.

        Comment

        • rski
          Recognized Expert Contributor
          • Dec 2006
          • 700

          #5
          You can use
          Code:
          strpos(string, substring)
          but it takes only two arguments.

          You can use strpos with substr and it works lile locate
          Code:
          locate(to_find,str,pos)=strpos(substr(str,pos),to_find)

          Comment

          Working...