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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:50:59+00:00 2026-05-29T11:50:59+00:00

I am trying check if a value is null if so the select null

  • 0

I am trying check if a value is null if so the select null else cast to numeric, but it throws an error. This is actually part of an insert statement

INSERT into someTable(name,created,power) 
SELECT 'xyz',now(),
case when :power ='null' then NULL else cast(:power as numeric) end from abc 

error that I get is

Error: ERROR: invalid input syntax for type numeric: "null"

:power is a variable that can be given any value using java code. If I give a value of null it give an error.

In code I get the following error from the java stack trace

org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to numeric
  • 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-29T11:51:00+00:00Added an answer on May 29, 2026 at 11:51 am

    Error

    SELECT CASE WHEN 'null' = 'null' THEN NULL ELSE cast('null' AS numeric) END 
    

    No error

    DO
    $$
    DECLARE
       power text := 'null';
    BEGIN
       PERFORM CASE WHEN power = 'null' THEN null ELSE cast(power AS numeric) END;
    END
    $$
    

    Explanation

    If you build a query string, the expression cast('null' AS numeric) or simply 'null'::numeric always raises an exception, even in an ELSE block that is never executed, because it is invalid input syntax and the exception is raised during the syntax check (like the error message implies), not during execution.

    A CASE statement like you display only makes sense with a parameter or variable not with literals. The second instance of the literal has no connection to the first instance whatsoever after the query string has been assembled.

    For dynamic SQL like that, you need to check the value before you build the query string. Or you use a function or prepared statement and pass the value as parameter. That would work, too.

    More advice after comment:

    In your particular case you could check the value in the app and build a query string like this:

    INSERT INTO tbl(name, abc_id, created, power) 
    SELECT 'xyz'
         , abc_id
         , now()
         , <insert_value_of_power_or_NULL_here> -- automatically converted to numeric
    FROM   abc;
    

    You may be interested in a different approach to INSERT data from a file conditionally.
    Use COPY for files local to the server or psql’s meta-command \copy for files local to the client.

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

Sidebar

Related Questions

i am trying to check the datagridview cells for empty and null value... but
I am trying to check the field(date field) value if it is null or
I'm trying to check if a form input has any value (doesn't matter what
Notice in this code I am trying to check for the existence of the
I am trying to create a BEFORE INSERT trigger that will check the incoming
I have this Servlet code, I am trying to check for duplicate usernames in
I am trying to check checkboxes based on a select option. So, if the
I'm doing the Agile Yii book. Anyway, I'm trying to execute this command: INSERT
I'm trying to emulate RapidCRC 's ability to check crc32 values within filenames on
I'm trying to check, using an automated discovery tool, when JAR files in remote

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.