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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:44:38+00:00 2026-06-02T23:44:38+00:00

Is it possible to define a default value that will be returned in case

  • 0

Is it possible to define a default value that will be returned in case a CAST operation fails?

For example, so that:

SELECT CAST('foo' AS INTEGER)

Will return a default value instead of throwing 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-02T23:44:40+00:00Added an answer on June 2, 2026 at 11:44 pm

    There is no default value for a CAST:

    A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:

    CAST ( expression AS type )
    expression::type
    

    There is no room in the syntax for anything other than the expression to be casted and the desired target type.

    However, you can do it by hand with a simple function:

    create or replace function cast_to_int(text, integer) returns integer as $$
    begin
        return cast($1 as integer);
    exception
        when invalid_text_representation then
            return $2;
    end;
    $$ language plpgsql immutable;
    

    Then you can say things like cast_to_int('pancakes', 0) and get 0.

    PostgreSQL also lets you create your own casts so you could do things like this:

    create or replace function cast_to_int(text) returns integer as $$
    begin
        -- Note the double casting to avoid infinite recursion.
        return cast($1::varchar as integer);
    exception
        when invalid_text_representation then
            return 0;
    end;
    $$ language plpgsql immutable;
    
    create cast (text as integer) with function cast_to_int(text);
    

    Then you could say

    select cast('pancakes'::text as integer)
    

    and get 0 or you could say

    select cast(some_text_column as integer) from t
    

    and get 0 for the some_text_column values that aren’t valid integers. If you wanted to cast varchars using this auto-defaulting cast then you’d have to double cast:

    select cast(some_varchar::text as integer) from t
    

    Just because you can do this doesn’t make it a good idea. I don’t think replacing the standard text to integer cast is the best idea ever. The above approach also requires you to leave the standard varchar to integer cast alone, you could get around that if you wanted to do the whole conversion yourself rather than lazily punting to the built in casting.

    NULL handling is left as an (easy) exercise for the reader.

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

Sidebar

Related Questions

Possible Duplicate: Objective-C Default Argument Value I couldn't find a way to define a
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Is it possible to define a regex which will match every character except a
Is it possible to define operators for enums? For example I have enum Month
Wondering if it's possible to change a value returned from a queryset before sending
In Crystal reports, you can define default values for the report parameters. For example,
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Is it possible to define an instance method in ruby from a string (the
Is it possible to define an array of text fields (or any data type)
Is it possible to define two sections in the web.xml in order to catch

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.