maruthi reddy
maruthi reddy

Reputation: 653

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) ??

Upvotes: 0

Views: 573

Answers (2)

van
van

Reputation: 77082

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')

Upvotes: 0

tuxuday
tuxuday

Reputation: 3037

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

check for more info

Upvotes: 1

Related Questions