MySQL's TRIM statement in sqlalchemy

154 views
Skip to first unread message

Robert Buchholz

unread,
Dec 26, 2014, 9:08:23 PM12/26/14
to sqlal...@googlegroups.com
Hello,

I am trying to write the following query in MySQL (in essence):
SELECT TRIM(TRAILING "-2" FROM c.slug)
FROM categories c;

In reality, the column this is run on is an anonymous aliased table in a
JOIN expression*. Is it possible to generate the TRIM(TRAILING X FROM Y)
with an SQLa function?


Cheers,

Robert



* The whole thing looks like this:
SELECT *
FROM categories c
JOIN categories c2
ON c.slug =
TRIM(TRAILING "-2" FROM c2.slug)
WHERE c2.slug LIKE "%-2";


Michael Bayer

unread,
Dec 26, 2014, 9:29:44 PM12/26/14
to sqlal...@googlegroups.com


Robert Buchholz <robert....@goodpoint.de> wrote:

> Hello,
>
> I am trying to write the following query in MySQL (in essence):
> SELECT TRIM(TRAILING "-2" FROM c.slug)
> FROM categories c;
>
> In reality, the column this is run on is an anonymous aliased table in a
> JOIN expression*. Is it possible to generate the TRIM(TRAILING X FROM Y)
> with an SQLa function?

this is a custom SQL format so you’d want to use @compiles. you can probably subclass ColumnElement. FunctionElement might work also, scan through http://docs.sqlalchemy.org/en/rel_0_9/core/compiler.html# for details.




>
>
> Cheers,
>
> Robert
>
>
>
> * The whole thing looks like this:
> SELECT *
> FROM categories c
> JOIN categories c2
> ON c.slug =
> TRIM(TRAILING "-2" FROM c2.slug)
> WHERE c2.slug LIKE "%-2";
>
>
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages