SQL Server TIMEFROMPARTS

Summary: in this tutorial, you will learn how to return a time value for the specified time with the specified precision.

Introduction to SQL Server TIMEFROMPARTS() function

The TIMEFROMPARTS() function returns a fully initialized time value. It requires five arguments as shown in the following syntax:

TIMEFROMPARTS ( hour, minute, seconds, fractions, precision )  
Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • hour – specifies hours.
  • minute – specifies minutes.
  • seconds – specifies seconds.
  • fractions – specifies fractions.
  • precision – specifies the precision of the time value. The precision cannot be null. If it is null, the function will raise an error.

The TIMEFROMPARTS() function returns a value of type time(precision)

SQL Server TIMEFROMPARTS() function example

Let’s take some examples of using the TIMEFROMPARTS() function.

A) Using TIMEFROMPARTS() function to return a time without fractions of a second

The following example shows how to use the TIMEFROMPARTS() function to return a time without fraction:

SELECT 
    TIMEFROMPARTS(23, 59, 59, 0, 0) AS Time;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

Time
----------------
23:59:59

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

B) Using TIMEFROMPARTS() function to return a time with fractions of a second

This example demonstrates how to use the TIMEFROMPARTS() function with the fractions and precision parameters:

SELECT 
    TIMEFROMPARTS(06, 30, 15, 5, 2) Time;
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

Time
----------------
06:30:15.05

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

In this example, when the fractions has a value of 5 and precision has a value of 2, then the value of fractions represents 5/100 or 0.05 of a second.

In this tutorial, you have learned how to use the SQL Server TIMEFROMPARTS() function to return a time value.

Was this tutorial helpful?