0

I have created a function to insert/update record in a table. Data is passed to function as arguments. Facing problem in handling Integer/float/double fields. For example, i have declared Empno field as Int(as per target table structure). So i thought when a null value comes i convert into '0' using cast function. But its not even accepting NULL values since its declared as Int. should i change the declaration to varchar and then convert ? What should i do ? Please help

Error i'm getting is operator does not exist: text integer 42883

Sample CODE:

create function test (text[])  
declare  
companycode varchar(4);  
empno varchar(10);  
begin  
L_1 := $1[1];  
L_2 :=cast(NULLIF($1[2],0) as int);  
update and insert statement ...  
end;  
3
  • 2
    Hi, could you please post the code for the function you've written? Commented Sep 30, 2019 at 15:53
  • 1
    Casting does not convert NULLs to zero. Commented Sep 30, 2019 at 15:54
  • updated with sample code Commented Sep 30, 2019 at 15:57

1 Answer 1

1

NULLIF() changes the specified value to NULL, not vica-versa. Use IFNULL() or COALESCE() instead.

Also Change the order of the operations to CAST first, something like this:

COALESCE($1[2]::int, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Usagi, Thank you for your support. I tried your suggestion for another float field(replaced int with float(8)) but i'm getting another error when i try to insert: column is of type double precision but expression is of type character varying Any suggestions ?
Hey Usagi, I managed to resolve the issue. Thank you for your timely response !

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.