1

When I am trying to use following update query...

UPDATE
            product_table
        SET
            product_name = $1,
            price = $2,
            delivery_interval = NULLIF ($3, '')
        WHERE
            id = $4;

...following appears...

error: column "delivery_interval" is of type integer but expression is of type text

delivery_interval = NULL instead of NULLIF ($3, '') seems to work. Why?

3
  • What type is $3? Commented Nov 19, 2021 at 10:56
  • Why is $3 a string instead of an integer? Looks like an application issue, that creates an error in the database. Commented Nov 19, 2021 at 11:47
  • $3 is an integer or a string when the input is empty. Commented Nov 19, 2021 at 12:10

3 Answers 3

4

As best as I can tell, you need this:

delivery_interval = (NULLIF($3::text, '')::integer)

First you need to cast the argument to text, so NULLIF is comparing like with like, then you cast the whole expression to integer, to match the column type.

Sign up to request clarification or add additional context in comments.

Comments

1

If it is Postgres Then you have to use NULL For integer Datatype

update table_name set status = 'SUCCESS', column2 = NULL where condition <>

Comments

0

From the documentation:

The result has the same type as the first argument — but there is a subtlety. What is actually returned is the first argument of the implied = operator, and in some cases that will have been promoted to match the second argument's type. For example, NULLIF(1, 2.2) yields numeric, because there is no integer = numeric operator, only numeric = numeric.

So either your $3 is text or is implicitly casted to text because of the implicit = nullif() causes. And that makes the result of nullif() a text.

You can try to explicitly cast the result of nullif().

...
nullif($3, '')::integer
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.