Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

661
Views
nodejs mysql insert null datetime field

using mysql-ssh

How to insert DateTime field's value to null in MySQL through nodejs?

Tried following:

connection.query('INSERT INTO idea_steps (field1, field2) VALUES(?, ?)', ['value1', null])
connection.query('INSERT INTO idea_steps (field1, field2) VALUES(?, ?)', ['value1', ''])
connection.query('INSERT INTO idea_steps (field1, field2) VALUES(?, ?)', ['value1', 'null'])
connection.query('INSERT INTO idea_steps (field1, field2) VALUES(?, ?)', ['value1', undefined])

Table Structure:

field1 String
field2 DateTime Default null

Error:

for ''

Incorrect datetime value: \'\' for column \'field2\' at row 730

for undefined

Unknown column \'NaN\' in \'field list\'

for 'null'

Incorrect datetime value: \'null\' for column \'field2\' at row 730'

for null

Unknown column \'NaN\' in \'field list\'

Can anyone help me to insert DateTime field to null? I can't skip the field2 field. Let me know if it is even possible, if not I'll have to figure out something else.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The syntax of connection.query seems faulty over here.

It should be:

const sqlQuery = "INSERT INTO idea_steps (field1, field2) VALUES ?";
const values =  [ ['field1', 'NULL'] ];

con.query(sqlQuery, [values], function (err, result) {
    if (err) throw err;
    console.log("Number of records inserted: " + result.affectedRows);
  });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!