Can I use a CASE statement within substr and filter?
Here is my requirement:
>> value = '00021050430' #here value is a database column
>> query.filter((func.substr(value,case([(func.length(value) > 7,func.length(varying_value)-7+1))],else_=1),7)=='1050430')
Output I am expecting is:
>> query.filter(func.substr(value,6,7))
The above throws an error.
You do not need to use a
CASE()statement here. Just use Python:This is called a conditional expression; it’s constructed as
true_expr if test_expr else false_expr, wheretrue_expris used if thetest_exprevaluates to True, otherwise thefalse_exprpart is used.If
valueis not a constant but a column reference, then you do need to use aCASE()statement. Usesqlalchemy.sql.expression.case()for that:but you need to make sure your
(and)parenthesis are balanced (you had one too many).