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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:32:31+00:00 2026-06-11T11:32:31+00:00

I’m using Postgresql DB. I want to perform a query against a numeric column

  • 0

I’m using Postgresql DB.

I want to perform a query against a numeric column in a table to find if the value starts with a particular number.

I currently have:

SELECT * FROM myTable WHERE to_char(ID, '12345678') LIKE '2%'

However this returns an empty dataset (but there are values in the ID column which start with 2.

Many thanks

Steve

  • 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-11T11:32:32+00:00Added an answer on June 11, 2026 at 11:32 am

    Don’t laugh. As an alternative to the casts, you could use a range query, like:

    SELECT * FROM mytable
    WHERE id = 2
    OR (id >=20 AND id < 30)
    OR (id >=200 AND id < 300)
    OR (id >=2000 AND id < 3000)
    OR (id >=20000 AND id < 30000)
    OR (id >=200000 AND id < 300000)
    OR (id >=2000000 AND id < 3000000)
    OR (id >=20000000 AND id < 30000000)
    OR (id >=200000000 AND id < 300000000)
    OR (id >=2000000000 AND id < 3000000000)
    OR (id >=20000000000 AND id < 30000000000)
    OR (id >=200000000000 AND id < 300000000000)
            -- ...
            ;
    

    This could cause postgres to generate a much better query plan, because an index can be used (if present, which can be expected for an id field)

    UPDATE:
    The query plans:

    Bitmap Heap Scan on reservations  (cost=1838.15..13290.11 rows=110809 width=4012) (actual time=11.310..24.379 rows=111111 loops=1)
       Recheck Cond: ((id = 2) OR ((id >= 20) AND (id < 30)) OR ((id >= 200) AND (id < 300)) OR ((id >= 2000) AND (id < 3000)) OR ((id >= 20000) AND (id < 30000)) OR ((id >= 200000) AND (id < 300000)) OR ((id >= 2000000) AND (id < 3000000)) OR ((id >= 20000000) AND (id < 30000000)) OR ((id >= 200000000) AND (id < 300000000)) OR ((id >= 2000000000) AND (id < 3000000000::bigint)) OR ((id >= 20000000000::bigint) AND (id < 30000000000::bigint)) OR ((id >= 200000000000::bigint) AND (id < 300000000000::bigint)))
       ->  BitmapOr  (cost=1838.15..1838.15 rows=112192 width=0) (actual time=11.242..11.242 rows=0 loops=1)
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.005..0.005 rows=1 loops=1)
                   Index Cond: (id = 2)
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.58 rows=10 width=0) (actual time=0.003..0.003 rows=10 loops=1)
                   Index Cond: ((id >= 20) AND (id < 30))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..2.52 rows=104 width=0) (actual time=0.013..0.013 rows=100 loops=1)
                   Index Cond: ((id >= 200) AND (id < 300))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..14.24 rows=1036 width=0) (actual time=0.110..0.110 rows=1000 loops=1)
                   Index Cond: ((id >= 2000) AND (id < 3000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..144.73 rows=10845 width=0) (actual time=1.050..1.050 rows=10000 loops=1)
                   Index Cond: ((id >= 20000) AND (id < 30000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1332.24 rows=100196 width=0) (actual time=10.013..10.013 rows=100000 loops=1)
                   Index Cond: ((id >= 200000) AND (id < 300000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.010..0.010 rows=0 loops=1)
                   Index Cond: ((id >= 2000000) AND (id < 3000000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
                   Index Cond: ((id >= 20000000) AND (id < 30000000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
                   Index Cond: ((id >= 200000000) AND (id < 300000000))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1)
                   Index Cond: ((id >= 2000000000) AND (id < 3000000000::bigint))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.026..0.026 rows=0 loops=1)
                   Index Cond: ((id >= 20000000000::bigint) AND (id < 30000000000::bigint))
             ->  Bitmap Index Scan on reservations_pkey  (cost=0.00..1.49 rows=1 width=0) (actual time=0.002..0.002 rows=0 loops=1)
                   Index Cond: ((id >= 200000000000::bigint) AND (id < 300000000000::bigint))
     Total runtime: 28.720 ms
    (28 rows)
    
    
     Seq Scan on reservations  (cost=0.00..19219.52 rows=4383 width=4012) (actual time=0.025..184.532 rows=111111 loops=1)
       Filter: ((id)::text ~~ '2%'::text)
     Total runtime: 189.100 ms
    (3 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.