Home > Database > Mysql Tutorial > How to Convert an Integer Representing Time to HH:MM:SS:00 Format in SQL Server 2008?

How to Convert an Integer Representing Time to HH:MM:SS:00 Format in SQL Server 2008?

Susan Sarandon
Release: 2024-12-31 04:48:10
Original
884 people have browsed it

How to Convert an Integer Representing Time to HH:MM:SS:00 Format in SQL Server 2008?

Convert Integer Time to HH:MM:SS:00 in SQL Server 2008

Question:

In SQL Server 2008, it is necessary to transform an integer time value into the time format HH:MM:SS:00. Additionally, it is essential to understand whether the '00' in this format represents milliseconds.

Answer:

To convert an integer time to HH:MM:SS:00, utilize the following steps:

  1. Extract Hour: Divide the integer time by 1,000,000 and take the modulus 100 to obtain the hour value.
  2. Extract Minute: Divide the remaining integer time by 10,000 and take the modulus 100 to obtain the minute value.
  3. Extract Second: Divide the remaining integer time by 100 and take the modulus 100 to obtain the second value.
  4. Extract Millisecond: Multiply the remaining integer time by 10 to obtain the millisecond value.

The '00' in the time format HH:MM:SS:00 represents milliseconds.

Example:

Consider the integer time 10455836.

SELECT
    (@T / 1000000) % 100 AS hour,
    (@T / 10000) % 100 AS minute,
    (@T / 100) % 100 AS second,
    (@T % 100) * 10 AS millisecond
Copy after login

Result:

hour    minute  second  millisecond
10      45      58      360
Copy after login

Alternatively, you can use the following query for a more comprehensive result:

SELECT
    DATENAME(HOUR, DATETIMEFROMPARTS(@T / 1000000 % 100, @T / 10000 % 100, @T / 100 % 100, @T % 100 * 10)) AS hour,
    DATENAME(MINUTE, DATETIMEFROMPARTS(@T / 1000000 % 100, @T / 10000 % 100, @T / 100 % 100, @T % 100 * 10)) AS minute,
    DATENAME(SECOND, DATETIMEFROMPARTS(@T / 1000000 % 100, @T / 10000 % 100, @T / 100 % 100, @T % 100 * 10)) AS second,
    DATENAME(MILLISECOND, DATETIMEFROMPARTS(@T / 1000000 % 100, @T / 10000 % 100, @T / 100 % 100, @T % 100 * 10)) AS millisecond
Copy after login

Output:

hour       minute   second   millisecond
10         45       58       360
Copy after login

This method also clearly identifies the time components as hour, minute, second, and millisecond.

The above is the detailed content of How to Convert an Integer Representing Time to HH:MM:SS:00 Format in SQL Server 2008?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template