MySQL TIMESTAMP()
The SQL function TIMESTAMP() is used in the SQL language to get a DATETIME from a DATE. The function can contain one or two arguments depending on the usage.
- With one argument: a date in DATE format is transformed into DATETIME
- With two arguments: a date and time will be transformed into a DATETIME
Syntax:
The syntax for using this function can be done in a SQL query as follows:
SELECT TIMESTAMP("YYYY-MM-DD");
Format of the result:
YYYY-MM-DD HH:MM:SS
SELECT TIMESTAMP("YYYY-MM-DD", "HH:MM");
Format of the result:
YYYY-MM-DD HH:MM:SS
The 2 queries have different usage.
Example :
A possible usage would be to get a date/time from a field that contains only a date.
SELECT TIMESTAMP("2022-10-16"); -- output: 2022-10-16 00:00:00
It can also happen that the date is stored in one column, while the time is stored in another column. This function would then be used to combine the date and time into one piece of information in DATETIME format.
SELECT TIMESTAMP("2022-10-16", "08:30"); -- output: 2022-10-16 08:30:00