简体   繁体   中英

Sequelize: Using LIKE on DATE

We are creating search functionality for our app. We need to be able to search on dates. For example, a user might want to find all records where the Sale Date is 2018. In our DB the Sale Date is a DATE type. Is there a way to use the LIKE keyword on DATE?

I'd ideally like the WHERE clause to look like this:

findAll({where: {SaleDate: {like: `%${searchString}%`}})

We are using SQLServer for our DB.

Do not use like on dates. like is for strings and strings are not dates.

In general, I think "start date" and "end date" is sufficient for most purposes. Every so often you may need customized approaches, such as for a particular day of the week or day of the month.

I using NODE and MongoDB, if date has been stored in DATE type. Then you ca use $year function of Mongo.

Here is the link, showing how to use $year https://docs.mongodb.com/manual/reference/operator/aggregation/year/

Some of the examples are :

{ $year: new Date("2016-01-01") } // 2016.

This can be used in find query to get the required result.

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