1

I am inserting a response from express api into a postgres table. I have created a table already with a column 'createdon' with type timestamp. In response, I am getting a timestamp which I want to insert in that field. This is the script I wrote:

client.query(`INSERT INTO table (col1, col2,createdon, col3) VALUES ($1, $2, $3, $4) RETURNING *`, [val1, val2, val3, val4])

This is working fine until I get a timestamp in createdon field. If I get empty response for createdon field, it's throwing this error.

How can I bypass this error?

0

1 Answer 1

2

If you want to insert a blank timestamp use null instead. With nullif you can test if the value is empty and replace it with null:

INSERT INTO table (col1, col2,createdon, col3) 
VALUES ($1, $2, NULLIF($3,'')::timestamp, $4) 
RETURNING *

Demo: db<>fiddle

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

1 Comment

Thanks, assigning timestamp to nullif worked. :). NULLIF($3,'')::timestamp

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.