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 9178473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:33:35+00:00 2026-06-17T17:33:35+00:00

I need to build a query in PostgreSQL and am required to find all

  • 0

I need to build a query in PostgreSQL and am required to find all text entries that contain a 6 digit number (e.g. 000999, 019290, 998981, 234567, etc). The problem is that the number is not necessary at the begining of the string or at its end.

I tried and didn’t work:

  • [0-9]{6} – returns part of a number with more than 6 digits
  • (?:(?<!\d)\d{6}(?!\d)) – postgresql does not know about lookbehind
  • [^0-9][0-9]{6}[^0-9] and variations on it, but to no avail.

Building my own Perl/C function is not really an option as I do not have the skills required. Any idea what regexp could be used or other tricks that elude me at the moment?

EDIT

Input samples:

  • aa 0011527 /CASA -> should return NOTHING
  • aa 001152/CASA -> should return 001152
  • aa001152/CASA -> should return 001152
  • aa0011527/CASA -> should return NOTHING
  • aa001152 /CASA -> should return 001152
  • 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-17T17:33:37+00:00Added an answer on June 17, 2026 at 5:33 pm

    If PostgreSQL supports word boundaries, use \b:

    \b(\d{6})\b
    

    Edit:

    \b in PostgreSQL means backspace, so it’s not a word boundary.

    http://www.postgresql.org/docs/8.3/interactive/functions-matching.html#FUNCTIONS-POSIX-REGEXP however, will explain you that you can use \y as a word boundary, as it means matches only at the beginning or end of a word, so

    \y(\d{6})\y
    

    should work.

    \m(\d{6})\M
    

    should also work.

    Full list of word matches in PostgreSQL regex:

    Escape  Description
    \A      matches only at the beginning of the string (see Section 9.7.3.5 for how this differs from ^)
    \m      matches only at the beginning of a word
    \M      matches only at the end of a word
    \y      matches only at the beginning or end of a word
    \Y      matches only at a point that is not the beginning or end of a word
    \Z      matches only at the end of the string (see Section 9.7.3.5 for how this differs from $)
    

    New edit:

    Based on your edit, you should be able to do this:

    (^|[^\d])(\d+)([^\d]|$)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a sqlserver query that if a text column has a
I'm trying to build a query in Postgresql that will be used for a
I need to build query in SQL to get rows the have text like
I need to build query, that return result with network topology with more than
I need to build a configurable query builder that allows the user to compound
I have read-only access to these tables, and need to build a query that
I need to build a query that will show me records that are in
I need to build a query that will aggregate the # of units of
Using ORMLite for Android, I need to build a query that returns orders by
I need to build an HQL query that uses the same entity twice, 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.