简体   繁体   中英

filter SqlAlchemy column value by number of resulting characters

How can I filter SqlAlchemy column by number of resulting Characters,

Here is a kind of implementation I am looking at,

query = query.filter(Take_Last_7_Characters(column_1) == '0321334')

Where "Take_Last_7_Characters" fetches the last 7 characters from the resulting value of column_1

So How can I implement Take_Last_7_Characters(column_1) ??

use sqlalchemy.sql.expression.func , to generate SQL functions.

check for more info

Please use the func to generate SQL functions as directed by @tuxuday.
Note that the code is RDBMS-dependant. The code below runs for SQLite , which offers SUBSTR and LENGTH functions. Your actual database might have different names for those ( LEN, SUSBSTRING, LEFT, RIGHT , etc).

qry = session.query(Test)
qry = qry.filter(func.SUBST(Test.column_1, func.LENGTH(Test.column_1) - 6, 7) == '0321334')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM