MySQL - MAKETIME() Function



The MySQL MAKETIME() function is used to create a time value from the given hours, minutes, and seconds.

The function accepts three numerical values representing hour, minute and second values as parameters (in the same order), creates a time value based on these values, and returns the result. If either of the arguments is NULL, the result is still NULL. Also, the seconds argument can have a fractional part.

Syntax

Following is the syntax of MySQL MAKETIME() function −

MAKETIME(hour,minute,second)

Parameters

This method accepts three parameters. The same is described below −

  • hour: The hour component of the time (0 to 23).

  • minute: The minute component of the time (0 to 59).

  • second: The second component of the time (0 to 59).

Return value

The function returns a time value representing the specified hour, minute, and second.

Example

Following example demonstrates the usage of the MAKETIME() function −

SELECT MAKETIME(13, 45, 25) As Result;

Output

This will produce the following result −

Result
13:45:25

Example

We can also pass arguments to this function as string values as shown below −

SELECT MAKETIME('09', '21', '34') As Result;

Output

Following is the output −

Result
09:21:34.000000

Example

Any of the arguments of this function can be 0. In the below query, we are passing seconds parameter as 0 −

SELECT MAKETIME(23, 24, 0) As Result;

Following is the output −

Result
23:24:00

Here, we are passing 0 to the months parameter −

SELECT MAKETIME(23, 0, 55) As Result;

Following is the output −

Result
23:00:55

Example

If either of the arguments of this function is NULL it returns NULL. In the below query, we are passing NULL to the seconds parameter −

SELECT MAKETIME(23, 24, NULL) As Result;

Following is the output −

Result
NULL

Here, we are passing NULL to the hours parameter of the function −

SELECT MAKETIME(NULL, 24, 55) As Result;

Following is the output −

Result
NULL

Example

In the following example, we are creating a table named SUBSCRIBERS using the CREATE statement as follows −

CREATE TABLE SUBSCRIBERS (
   SUBSCRIBERNAME varchar(255),
   PACKAGENAME varchar(255),
   HOUR int,
   MINUTE int,
   SECOND int
);

Now, we will insert 5 records in SUBSCRIBERS table using INSERT statements −

INSERT INTO SUBSCRIBERS VALUES
('Rahul', 'Premium', 20, 53, 49),
('Aadhya', 'Basic', 10, 13, 19),
('Nikhil', 'Moderate', 05, 43, 20),
('Maaya', 'Basic', 16, 36, 39),
('Varshu', 'Premium', 12, 45, 45);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From SUBSCRIBERS;

Following is the SUBSCRIBERS table −

SUBSCRIBERNAME PACKAGENAME HOUR MINUTE SECOND
Rahul Premium 20 53 49
Aadhya Basic 10 13 19
Nikhil Moderate 5 43 20
Maaya Basic 16 36 39
Varshu Premium 12 45 45

Here, we are using the MySQL MAKETIME() function to combine values from the "Hour," "Minute," and "Second" columns to represent the subscription time −

SELECT SubscriberName, PackageName, MAKETIME(Hour, Minute, Second)
As SubscriptionTime FROM SUBSCRIBERS;

Output

The output is displayed as follows −

SUBSCRIBERNAME PACKAGENAME SubscriptionTime
Rahul Premium 20:53:49
Aadhya Basic 10:13:19
Nikhil Moderate 05:43:20
Maaya Basic 16:36:39
Varshu Premium 12:45:45
Advertisements