Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:21:21+00:00 2026-06-15T11:21:21+00:00

Can I use a CASE statement within substr and filter ? Here is my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:21:22+00:00Added an answer on June 15, 2026 at 11:21 am

    You do not need to use a CASE() statement here. Just use Python:

    query.filter(func.substr(value, 1 if len(value) > 7 else 1, 7) == '1050430')
    

    This is called a conditional expression; it’s constructed as true_expr if test_expr else false_expr, where true_expr is used if the test_expr evaluates to True, otherwise the false_expr part is used.

    If value is not a constant but a column reference, then you do need to use a CASE() statement. Use sqlalchemy.sql.expression.case() for that:

    from sqlalchemy.sql.expression import case
    
    query.filter(func.substr(value, case([(func.length(column) > 7, func.length(column) - 7 + 1)], else_=1), 7) == '1050430')
    

    but you need to make sure your ( and ) parenthesis are balanced (you had one too many).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i use a nested form of the 'case' statement in a sql
How can I create a case statement (or multiple if statements) within a loop
To do a crosstab/pivot in sqlite one can use the case statement as described,
Can I use a table column within a Like operator? I've created an example,
The use case I can think of is the following: In a typical application,
A very simple use case for creating users in webapp2/appengine can be as follows:
How can I use output caching with a .ashx handler? In this case I'm
I use VS2008 targetting .NET 2.0 Framework, and, just in case, no I can't
Ok, I would like to use a CASE STATEMENT for this, but I am
Is there a method to use contain rather than equal in case statement? For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.