Home > Software engineering >  Time from parts
Time from parts

Time:11-03

Select TimeFromParts(FieldName/ 10000,(FieldName / 100 % 100, % 100, 0, 0)

Could someone explain this select statement and what exactly each argument is doing? I see this is used to change an integer to time but I'm curious as to what exactly is going on and why those numbers were chosen.

I somewhat understand the last 2 zeros are for the fraction and precision.

CodePudding user response:

Quoting Microsoft documentation:

TIMEFROMPARTS (Transact-SQL) Returns a time value for the specified time and with the specified precision.

Syntax:

TIMEFROMPARTS ( hour, minute, seconds, fractions, precision )  

Arguments:

hour
Integer expression specifying hours.

minute
Integer expression specifying minutes.

seconds
Integer expression specifying seconds.

fractions
Integer expression specifying fractions.

precision
Integer literal specifying the precision of the time value to be returned.

More info you find at source. The numbers in your query are not telling anything. But the quoted formula explains what TIMEFROMPARTS expects as input arguments.

CodePudding user response:

The query that you posted unfortunately is incomplete, but as Amikot40 stated, the TIMEFROMPARTS function will create a value out of an array of 5 inputs. As for the percentage sign: this is called a modulo. A modulo or mod is the remainder after dividing one number by another. Example: 100 mod 9 equals 1. Because 100/9 = 11 with a remainder of 1.

In this case, the modulo is used to extract the remaining seconds after determining the amount of seconds were used to create full hours. Further down the road it's used to determine the amount of seconds remaining after determining the amount of seconds used to create full minutes. Finally, what remains are the seconds left that were not part of the hour / minute combination.

  • Related