Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trunc date field in mysql like Oracle

Tags:

date

mysql

oracle

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

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'?

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.

like image 436
Prajith A S Avatar asked Oct 12 '14 06:10

Prajith A S


People also ask

What is TRUNC in MySQL?

Definition and Usage The TRUNCATE() function truncates a number to the specified number of decimal places. Note: See also the FLOOR(), CEIL(), CEILING(), and ROUND() functions.

What is the TRUNC function in Oracle?

The TRUNC (number) function returns n1 truncated to n2 decimal places. If n2 is omitted, then n1 is truncated to 0 places. n2 can be negative to truncate (make zero) n2 digits left of the decimal point.

How can get date in dd mm yyyy format in MySQL?

MySQL DATE_FORMAT() Function The DATE_FORMAT() function formats a date as specified.


2 Answers

Use DATE(expr) function. Query example:

SELECT *
  FROM dlbcc_purchase
  WHERE DATE(due_date) = '2014-11-20'
like image 125
Rimas Avatar answered Oct 19 '22 13:10

Rimas


You can use DATE_FORMAT().

example:

select * from dlbcc_purchase where DATE_FORMAT(due_date,'%d-%b-%Y') = '20-nov-2014' 
like image 5
Dens Avatar answered Oct 19 '22 13:10

Dens