MySQL: SUBTIME() Function Examples

MySQL: SUBTIME() Function Examples

Subtime() function in MySQL; Through this tutorial, we will learn how to subtract date, time, fraction seconds, etc. from given datetime / time value in MySQL subtime() function with the help of example.

In MySQL SUBTIME() function tutorial, we would love to share with MySQL SUBTIME() function with its syntax, definition, parameters, and several examples.

MySQL SUBTIME() function

Definition:– SUBTIME() function is built-in MySQL function, which is commonly used to subtract a time value from a time or datetime value or exp.

Here, exp is expression.

Syntax

The syntax of subtime() function is following:

 SUBTIME(expression1,  expression2)

Parameters of subtime() function is following:

  • expression1:- can be either a time or DateTime expression
  • expression2 is a time expression.

Example 1 – Simple Usage

Here, We will take an example to demonstrate substime() function.

SELECT SUBTIME('11:35:00', '1:05');

The output of the above query is:

 +--------------------------------+
 |  SUBTIME('11:35:00', '1:05')   |
 +--------------------------------+
 | 10:30:00                       |
 +--------------------------------+

Example 2 – Subtracting Seconds In Time Value

Here, we will take a second example, we also subtract a number of seconds from the time value.

SELECT SUBTIME('11:35:00', '1:05:30');

The output of the above query is:

 +--------------------------------+
 | SUBTIME('11:35:00', '1:05:30') |
 +--------------------------------+
 |  10:29:30                      |
 +--------------------------------+

Example 3 – Subtract Fractional Seconds In Time Value

Here, we will take a third example, we also subtract a number of fractional seconds from the time value.

SELECT SUBTIME('12:35:00.888888', '1:30:30.555555'); 

The output of the above query is:

 +----------------------------------------------+
 | SUBTIME('11:40:00.888888', '2:40:20.444444') |
 +----------------------------------------------+
 | 08:59:40.444444                              |
 +----------------------------------------------+

Example 4 – Subtract from a DateTime Value

Here, we will take the fourth example of subtime function, in this example, we subtract DateTime value.

SELECT SUBTIME('2020-01-04 11:40:00', '2:40:40');

The output of the above query is:

 +-------------------------------------------+
 | SUBTIME('2020-01-04 11:40:00', '2:40:40') |
 +-------------------------------------------+
 | 2020-01-04 08:59:20                       |
 +-------------------------------------------+

Recommended Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *