
- 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
Create DATETIME from DATE and TIME in MySQL?
You can create DATETIME from DATE and TIME with the help of ADDTIME() function in MySQL. The syntax is as follows −
SELECT ADDTIME(CONVERT(yourDateColumnName,datetime),yourTimeColumnName) as anyVariableName from yourTableName;
To understand the above concept, let us create a table. The query to create a table is as follows −
mysql> create table DateTime −> ( −> DueDate date, −> DueTime time −> ); Query OK, 0 rows affected (1.19 sec)
Now you can insert date and time separately. The query to insert is as follows −
mysql> insert into DateTime values(curdate(),now()); Query OK, 1 row affected (0.08 sec)
Display all records from the table with the help of select statement −
mysql> select *from DateTime;
The following is the output −
+------------+----------+ | DueDate | DueTime | +------------+----------+ | 2018-12-11 | 09:59:07 | +------------+----------+ 1 row in set (0.00 sec)
Now you can implement the syntax we discussed in the beginning to create a datetime from date and time. The query is as follows −
mysql> select addtime(convert(DueDate,datetime),DueTime) as DATETimeDemo from DateTime;
The following is the output displaying datetime created from date and time −
+---------------------+ | DATETimeDemo | +---------------------+ | 2018-12-11 09:59:07 | +---------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to part DATE and TIME from DATETIME in MySQL?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- MySQL strip time component from datetime?
- How to convert JS date time to MySQL datetime?
- Get only the date from datetime in MySQL?
- How to combine date and time from different MySQL columns to compare with the entire DateTime?
- MySQL Query to convert from datetime to date?
- How to split the datetime column into date and time and compare individually in MySQL?
- How to compare DateTime Column with only Date not time in MySQL?
- Compare DATE string with string from MySQL DATETIME field?
- How to select only MySQL date from datetime column?
- Write a program to separate date and time from the datetime column in Python Pandas
- Changing data type from date to date/time in MySQL?
- Extracting only date from datetime field in MySQL and assigning it to PHP variable?
- Insert current date in datetime format MySQL?

Advertisements