简体   繁体   中英

Convert varchar column to date in Sequelize

I need to perform a query to a model that has column datetime saved in character varying here is my query

const _from = new Date(from).getTime();
Model.findAll({where: Sequelize.where(Sequelize.fn('datetime', Sequelize.col('start')), '>=', _from)})

with the above query this is the response: function datetime(character varying) does not exist

I have also try the following:

const _from = new Date(from);
Model.findAll({where: Sequelize.where(Sequelize.fn('date', Sequelize.col('start')), '>=', from)})

and with the above query this is the response: date/time field value out of range: 1495828800000

this is a screenshot of the columns in the table, I'm using postgress and Sequelize in Nodejs:

在此处输入图片说明

Try this code :

For sequelize >= 4.12.0

const Op = Sequelize.Op;
const _from = new Date(from).getTime();
Model.findAll({where: { start : {[Op.gte]: _from}})

For sequelize < 4.12.0 :

const _from = new Date(from).getTime();
Model.findAll({where: { start : {$gte: _from}})

Just by curiosity, why you store a date in a VARCHAR ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM