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?
CodePudding user response:
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,''), $4)
RETURNING *
Demo: db<>fiddle