
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to split the datetime column into date and time and compare individually in MySQL?
Let us first create a table −
mysql> create table DemoTable805(LoginDate datetime); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable805 values('2019-01-31 12:45:20'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable805 values('2017-11-01 10:20:30'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable805 values('2016-03-12 04:10:00'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable805 values('2018-12-24 05:01:00'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable805;
This will produce the following output −
+---------------------+ | LoginDate | +---------------------+ | 2019-01-31 12:45:20 | | 2017-11-01 10:20:30 | | 2016-03-12 04:10:00 | | 2018-12-24 05:01:00 | +---------------------+ 4 rows in set (0.00 sec)
Here is the query to split the DateTime column into date and time and compare individually −
mysql> select cast(LoginDate AS date),cast(LoginDate AS Time) from DemoTable805;
This will produce the following output −
+-------------------------+-------------------------+ | cast(LoginDate AS date) | cast(LoginDate AS Time) | +-------------------------+-------------------------+ | 2019-01-31 | 12:45:20 | | 2017-11-01 | 10:20:30 | | 2016-03-12 | 04:10:00 | | 2018-12-24 | 05:01:00 | +-------------------------+-------------------------+ 4 rows in set (0.03 sec)
- Related Articles
- How to compare DateTime Column with only Date not time in MySQL?
- How to combine date and time from different MySQL columns to compare with the entire DateTime?
- How to part DATE and TIME from DATETIME in MySQL?
- Combine date and time column into a timestamp in MySQL?
- Create DATETIME from DATE and TIME in MySQL?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- How to convert JS date time to MySQL datetime?
- How to compare the first date and the last date from a MySQL column with date records?
- How to select only MySQL date from datetime column?
- Concatenate date and time from separate columns into a single column in MySQL
- Write a program to separate date and time from the datetime column in Python Pandas
- How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?
- Convert from varchar to datetime and compare in MySQL?
- Compare DATE string with string from MySQL DATETIME field?
- How to add time in a MySQL column set with type DATETIME?

Advertisements