简体   繁体   English

续集:在DATE使用LIKE

[英]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. 例如,用户可能想查找销售日期为2018年的所有记录。在我们的数据库中,销售日期为DATE类型。 Is there a way to use the LIKE keyword on DATE? 有没有办法在DATE使用LIKE关键字?

I'd ideally like the WHERE clause to look like this: 理想情况下,我希望WHERE子句看起来像这样:

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

We are using SQLServer for our DB. 我们正在为数据库使用SQLServer。

Do not use like on dates. 不要在日期上使用like like is for strings and strings are not dates. like是用于字符串,而字符串不是日期。

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. 如果日期已存储为DATE类型,则使用NODE和MongoDB。 Then you ca use $year function of Mongo. 然后,您可以使用Mongo的$ year函数。

Here is the link, showing how to use $year https://docs.mongodb.com/manual/reference/operator/aggregation/year/ 这是链接,显示了如何使用$ year https://docs.mongodb.com/manual/reference/operator/aggregation/year/

Some of the examples are : 一些示例是:

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

This can be used in find query to get the required result. 可以在查找查询中使用它来获取所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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