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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:36:58+00:00 2026-05-27T10:36:58+00:00

Is it possible to order result rows by a varchar column cast to integer

  • 0

Is it possible to order result rows by a varchar column cast to integer in Postgres 8.3?

  • 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-27T10:36:59+00:00Added an answer on May 27, 2026 at 10:36 am

    It’s absolutely possible.

    ORDER BY varchar_col::int
    

    Be sure to have valid integer literals in your varchar column for each entry or you get an exception invalid input syntax for integer. Leading and trailing white space is ok – that’s trimmed automatically.

    If that’s the case, though, then why not convert the column to integer to begin with? Smaller, faster, cleaner, simpler.

    How to avoid exceptions?

    Postgres 16 or newer

    If the input might not be valid integer literals, use the dedicated function pg_input_is_valid() to test without raising an exception:

    ORDER BY CASE WHEN pg_input_is_valid(varchar_col, 'integer') THEN varchar_col::int END
    

    SQL CASE defaults to null in the absence of ELSE – which sorts last in default ascending order. See:

    • Sort by column ASC, but NULL values first?

    The new built-in function is faster and more reliable than below manual solution. Also different in that it does not try to fix broken input.

    Postgres 15 or older

    To remove non-digit characters before the cast and thereby avoid possible exceptions:

    ORDER BY NULLIF(regexp_replace(varchar_col, '\D', '', 'g'), '')::int
    
    • The regexp_replace() expression effectively removes all non-digits, so only digits remain or an empty string. (See below.)

    • \D is shorthand for the character class [^[:digit:]], meaning all non-digits ([^0-9]).
      In old Postgres versions with the outdated setting standard_conforming_strings = off, you have to use Posix escape string syntax E'\\D' to escape the backslash \. This was default in Postgres 8.3, so you’ll need that for your outdated version.

    • The 4th parameter g is for "globally", instructing to replace all occurrences, not just the first.

    • You may want to allow a leading dash (-) for negative numbers.

    • If the the string has no digits at all, the result is an empty string which is not valid for a cast to integer. Convert empty strings to NULL with NULLIF. (You might consider 0 instead.)

    The result is guaranteed to be valid. This procedure is for a cast to integer as requested in the body of the question, not for numeric as the title mentions.

    How to make it fast?

    One way is an index on an expression.

    CREATE INDEX tbl_varchar_col2int_idx ON tbl
    (cast(NULLIF(regexp_replace(varchar_col, '\D', '', 'g'), '') AS integer));
    

    Then use the same expression in the ORDER BY clause:

    ORDER BY
    cast(NULLIF(regexp_replace(varchar_col, '\D', '', 'g'), '') AS integer)
    

    Test with EXPLAIN ANALYZE whether the functional index actually gets used.

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

Sidebar

Related Questions

Is it possible to order return rows with a criteria on a sum group
Is it possible to order an NSMutableArray where it's values are NSStrings? I'm looking
Possible Duplicate: Case Order by using Null I'm looking to get a list of
Possible Duplicate: SQL ORDER BY total within GROUP BY UPDATE: I've found my solution,
Possible Duplicate: Elements order - for (... in ...) loop in javascript Assume you
Possible Duplicate: Choose order to execute JUnit tests I am writing a JUnit test.
Possible Duplicate: PHP - reversed order in if statement Checking for null - what
Possible Duplicate: Ordering by the order of values in a SQL IN() clause With
Possible Duplicate: Natural Sort Order in C# I have a list with a lot
Possible Duplicate: How to get the sort order in Delphi as in Windows Explorer?

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.