Bazinga777
Bazinga777

Reputation: 5291

Creating a date and time column from datetime column in mysql

Is it possible to extract date and time to seperate columns from a datetime column in mysql. I have looked around and its possible to do the exact opposite. So is there a way to do this?

Upvotes: 0

Views: 78

Answers (2)

VMai
VMai

Reputation: 10346

The date and time functions of MySQL come in handy:

SELECT DATE(yourDatetimeColumn), TIME(yourDatetimeColumn) FROM yourTable;

For the details you should look at DATE and TIME in the manual.

With STR_TO_DATE you get the right data type for the computed columns:

SELECT
    STR_TO_DATE(DATE(yourDatetimeColumn), '%Y-%m-%d') As yourDate,
    STR_TO_DATE(TIME(yourDatetimeColumn), '%k:%i') As yourTime
FROM
    yourTable;

Upvotes: 0

Rahul
Rahul

Reputation: 77926

Yes, it's possible. You use DAY(), HOUR,Minute functions accordingly. See Date and Time Functions

An example would be like

select DAY(NOW()) , hour(NOW())

Upvotes: 1

Related Questions