简体   繁体   English

使用LIKE函数查询日期的MYSQL

[英]MYSQL query using LIKE function for date

I have the following statement in the where statement of a mysql query: 我在mysql查询的where语句中有以下语句:

WHERE scrap_date LIKE '%-01-%'

I want to grab all the data with the scrap_date being in January. 我想使用scrap_date在一月份获取所有数据。 The error I recieve is: 我收到的错误是:

"Incorrect datetime value: '%-01-%' for column 'scrap_date' at row 1" “错误的日期时间值:第1行的'scrap_date'列的'%-01-%'”

The datatype for scrap_date is DATETIME . scrap_date的数据类型为DATETIME Not sure what syntax to use to get data with the a date in January, any suggestions? 不知道要使用什么语法来获取日期为一月的数据,有什么建议吗?

You are assuming the date is represented internally as a string. 您假设日期在内部以字符串形式表示。 It is not. 它不是。

Since its a DateTime, use the MONTH function to extract the month and compare it to the desired value 由于其为DateTime,因此使用MONTH函数提取月份并将其与所需值进行比较

WHERE MONTH(scrap_date) = 1

您可以尝试以下方法:

select  * from your table WHERE MONTH(scrap_date) = 1

You can use the DATEPART() function 您可以使用DATEPART()函数

SELECT * FROM table
WHERE  (DATEPART(mm, scrap_date) = 01)

使用月份功能

WHERE MONTH(scrap_date) = 1

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

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