Re: Converting from MySQL

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Ertan Küçükoğlu <ertan(dot)kucukoglu(at)1nar(dot)com(dot)tr>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: Converting from MySQL
Date: 2019-01-22 21:42:25
Message-ID: CAKFQuwayxz1DJcvaTccXzRYPZaU3geMV-7TBAZFL3NXHDAkE8g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Jan 22, 2019 at 2:21 PM Ertan Küçükoğlu
<ertan(dot)kucukoglu(at)1nar(dot)com(dot)tr> wrote:
> Failed to find PostgreSQL equivalent for below one because I did not
> understand what it does in the first place.
>
> SELECT
> CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/')
> FROM users WHERE email='%s'

PostgreSQL doesn't have a function named substring_index (it does have
concat, and a concat_ws variant, plus the || operator)

I believe the following demonstrates equivalent functionality for the
substring_index function - you could turn it into function of the same
name if you so choose.

select split_part(v, '@', 1), right(v, -(length(split_part(v, '@', 1))+1))
from ( values ('abc(at)123'), ('abc(at)123@%%%') ) vals (v)

The MySQL documentation explains what substring_index is doing - in
this case you need to get the "left and right side" components
separately via PostgreSQL functions for which I choose split_part and
right. A similar result can be had via regular expressions in a more
succinct (though not necessarily faster) way; or less succinctly via
substring.

If it wasn't for the possibility for having multiple "@" in an email
address split_part(,1) and split_part(,2) would be sufficient by
itself.

David J.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Gierth 2019-01-23 06:38:55 Re: Converting from MySQL
Previous Message Ertan Küçükoğlu 2019-01-22 21:21:03 Converting from MySQL