use of replace

450 views
Skip to first unread message

Horcle

unread,
Dec 28, 2015, 12:49:38 PM12/28/15
to sqlalchemy
I am iteratively building a complex query. For one step, I have 

a[i] = a[i].filter(Clinical.string_value.op('=')([value[i]])).subquery()

I would like to simply just replace the value in Clinical.string value such that all spaces are turned into underscores. My naive approach is to do this as 

replace(Clinical.string_value, ' ', '_'), but I know the syntax is invalid in this context. How do I plug this into my SQLAlch statement? I found the replace method here: http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.func, but it does not seem to fit my use case.

Thanks in advance!

Greg--

Horcle

unread,
Dec 28, 2015, 2:16:36 PM12/28/15
to sqlalchemy
Or something along the lines of

Clinical.string_value.replace(' ', '_').op...
Message has been deleted

Horcle

unread,
Dec 28, 2015, 4:45:48 PM12/28/15
to sqlalchemy
Looking for something like:

Select * from Clinical 
where replace(Clinical.string_value, ' ', '_') = 'value_here'

Mike Bayer

unread,
Dec 28, 2015, 10:19:22 PM12/28/15
to sqlal...@googlegroups.com
if you're using Postgresql you'd want to look into replace():
http://www.postgresql.org/docs/9.3/static/functions-string.html

e.g.

query.filter(func.replace(MyClass.column, ' ', '_') == 'some_other_value')

though if 'someothervalue' is an in-Python value and your database
doesn't have underscores already, you might be able to get away with
just using Python replace on that side, converting from underscore to space.


On 12/28/2015 04:29 PM, Horcle wrote:
> Looking for something to mimic the replace function in an SQL statement,
> like:
>
> Select * from Clinical where replace(Clinical.string_value, ' ' , '_')
>
>
> On Monday, December 28, 2015 at 11:49:38 AM UTC-6, Horcle wrote:
>
> I am iteratively building a complex query. For one step, I have
>
> a[i] = a[i].filter(Clinical.string_value.op('=')([value[i]])).subquery()
>
>
> I would like to simply just replace the value in Clinical.string
> value such that all spaces are turned into underscores. My naive
> approach is to do this as
>
> replace(Clinical.string_value, ' ', '_'), but I know the syntax is
> invalid in this context. How do I plug this into my SQLAlch
> statement? I found the replace method
> here: http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.func
> <http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.func>,
> but it does not seem to fit my use case.
>
> Thanks in advance!
>
> Greg--
>
> --
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Horcle

unread,
Dec 28, 2015, 10:32:57 PM12/28/15
to sqlalchemy
Using MySQL. I was missing the 'func' attribute syntax for calling this in my attempts. Works as desired.

Thanks!

Greg--



Reply all
Reply to author
Forward
0 new messages