Reputation: 41
I am trying to update SQL Server 2012 via sequelize CRM and getting the below error. My datatype for the column ins DateTime.
(node:33100) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: Conversion failed when converting date and/or time from character string.
Configuration
const Sequelize = require('sequelize');
const config = require('../config/config');
// Override timezone formatting for MSSQL
Sequelize.DATE.prototype._stringify = function _stringify(date, options) {
return this._applyTimezone(date, options).format('YYYY-MM-DD HH:mm:ss.SSS');
};
class SequelizeConnection {
connect() {
const sequelize = new Sequelize(config.database, config.username, config.password, config.options);
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
return sequelize
}
get() {
return this.connect()
}
}
const sequelize = new SequelizeConnection().get()
module.exports = sequelize;
in my model as below...
const { Sequelize } = require("sequelize");
const sequelize = require("../db/sequelize");
const User = sequelize.define('DB_USER', {
...... ....
DATE_OF_BIRTH: {
type: Sequelize.DATE,
notEmpty: true,
allowNull: false,
},
.....
module.exports = User
My update command to the table as follows
else if (arg.table === 'users') {
console.log(`table type is users ${arg.id} - ${arg.table}`)
if (arg.type === 0) {
User.update(data, {
where: { USER_ID: arg.id },
}).then(e => {}).then(e => {
event.sender.send('asynchronous-reply', _sync());
})
}
Upvotes: 0
Views: 1612
Reputation: 754258
There are many string formats supported for date & time by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not. And also, the DATETIME
datatype is notoriously picky about what string literals it considers valid dates×.
The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.
The ISO-8601 format is supported by SQL Server comes in two flavors:
YYYYMMDD
for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD
is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!or:
YYYY-MM-DDTHH:MM:SS
for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T
as delimiter between the date and time portion of your DATETIME
.This is valid for SQL Server 2000 and newer.
If you use SQL Server 2008 or newer and the DATE
datatype (only DATE
- not DATETIME
!), then you can indeed also use the YYYY-MM-DD
format and that will work, too, with any settings in your SQL Server.
Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD
format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.
The recommendation for SQL Server 2008 and newer is to use DATE
if you only need the date portion, and DATETIME2(n)
when you need both date and time. You should try to start phasing out the DATETIME
datatype if ever possible
Upvotes: 1