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

  • Home
  • SEARCH
  • 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 6611939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:10+00:00 2026-05-25T20:02:10+00:00

I’d like to select data from a table with the following rule, but I’m

  • 0

I’d like to select data from a table with the following rule, but I’m having trouble writing the query. I’m using PostgreSQL and I can’t create a UDF. The table looks like this:

id  | user_id  | account_number
-------------------------------
 1  |    1     | 12345671
 2  |    4     | 12356673
 3  |    7     | 12325678

The id and user_id are integers whereas the account number is a string. I’d like to select the account numbers that match the following conditions:

  • Account number string contains exactly 8 digits
  • Validation scheme
    1. Take the first 7 digits
    2. Multiply the first digit by 1, the second by 2, the third by 3, the fourth by 1, the fifth by 2, the sixth by 3 and the seventh by 1
    3. Sum the result of multiplying each digit by the relevant number
    4. If the 8th digit is the same as mod(sum, 10) then select this number

In this table above, I should only return the first two rows with the query.

Just to repeat, I can’t create a UDF, so am looking to find out whether this is possible using ordinary SQL in a query.

Thanks!

  • 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-05-25T20:02:11+00:00Added an answer on May 25, 2026 at 8:02 pm

    Yes, you can do it. Basically, use SIMILAR TO to check for exactly 8 digits, then substring and cast to do the math. Something like this:

    SELECT * FROM table_name WHERE
      account_number SIMILAR TO '[0-9]{8}'
      AND (
        1 * CAST(SUBSTR(account_number, 1, 1) AS INTEGER) +
        2 * CAST(SUBSTR(account_number, 2, 1) AS INTEGER) +
        3 * CAST(SUBSTR(account_number, 3, 1) AS INTEGER) +
        1 * CAST(SUBSTR(account_number, 4, 1) AS INTEGER) +
        2 * CAST(SUBSTR(account_number, 5, 1) AS INTEGER) +
        3 * CAST(SUBSTR(account_number, 6, 1) AS INTEGER) +
        1 * CAST(SUBSTR(account_number, 7, 1) AS INTEGER)
      )%10 = CAST(SUBSTR(account_number, 8, 1) AS INTEGER)
    

    Of course, this returns no rows in your example, because:

    1×1 + 2×2 + 3x3 + 1×4 + 2×5 + 3×6 + 1×7 = 53
    53 MOD 10 = 3
    3 ≠ 1
    

    PS: You do realize the UDFs can be written in languages other than C. E.g., you can write one in PL/pgSQL.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but

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.