Uziel Valdez
Uziel Valdez

Reputation: 2280

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:

enter image description here

Upvotes: 2

Views: 2097

Answers (1)

Arkerone
Arkerone

Reputation: 2026

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?

Upvotes: 1

Related Questions