Sjemmie
Sjemmie

Reputation: 1299

How to select date and time without the seconds in mysql?

How do I select date and time without the seconds in mysql from a column with date value in a table ? "YYYY-MM-DD HH:MM:SS" should be "YYYY-MM-DD HH:MM"

Upvotes: 32

Views: 50600

Answers (3)

Salman Arshad
Salman Arshad

Reputation: 272236

The proper solution is zero out seconds - and - preserve data type:

SELECT `datetime` - INTERVAL EXTRACT(SECOND FROM `datetime`) SECOND
FROM `some table`

If your datetime column contains microseconds then use this variant:

SELECT `datetime` - INTERVAL EXTRACT(SECOND_MICROSECOND FROM `datetime`) SECOND_MICROSECOND
FROM `some table`

SQL Fiddle

In theory, it should be faster than examples using date formatting functions.

Upvotes: 7

CristiC
CristiC

Reputation: 22698

SELECT DATE_FORMAT('2011-07-16 22:23:15', '%Y-%m-%d %H:%i');

Upvotes: 7

Ruslan Polutsygan
Ruslan Polutsygan

Reputation: 4461

SELECT DATE_FORMAT(`date`, '%Y-%m-%d %H:%i') AS `formatted_date` FROM `table`;

Upvotes: 78

Related Questions