MySQL 像Oracle一样在mysql中截断日期字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26322398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:06:42  来源:igfitidea点击:

Trunc date field in mysql like Oracle

mysqloracledate

提问by Prajith A S

I am to not able to use the 'trunc(in oracle)' function in 'mysql' database. I have a table called dlb_cc_purchase and date field called due_date in my 'mysql' database. The data displaying in the date field like 20-11-2014 00:00:00(20-nov-2014). in oracle we are using query

我不能在 'mysql' 数据库中使用 'trunc(in oracle)' 函数。我的“mysql”数据库中有一个名为 dlb_cc_purchase 的表和一个名为 Due_date 的日期字段。日期字段中显示的数据如20-11-2014 00:00:00(20-nov-2014)。在oracle中我们使用查询

select * from dlbcc_purchase where trunc(due_date) = '20-nov-2014' 

Oracle DB will fetch the row with due date 20-11-2014 00:00:00. How can I use this function in 'mysql'?

Oracle DB 将获取截止日期为 20-11-2014 00:00:00 的行。如何在“mysql”中使用此功能?

I know this is a basic question, but i was trying to do this for long time with truncate, str_to_date... but not able to fetch value. Please help.

我知道这是一个基本问题,但我长时间尝试使用 truncate、str_to_date...但无法获取值。请帮忙。

回答by Rimas

Use DATE(expr)function. Query example:

使用DATE(expr)函数。查询示例:

SELECT *
  FROM dlbcc_purchase
  WHERE DATE(due_date) = '2014-11-20'

回答by Dens

You can use DATE_FORMAT().

您可以使用DATE_FORMAT()

example:

例子:

select * from dlbcc_purchase where DATE_FORMAT(due_date,'%d-%b-%Y') = '20-nov-2014'