Anyname Donotcare
Anyname Donotcare

Reputation: 11393

How to convert number of hours to time

If i have workhours as an int represent the number of hours .

How to convert this number of hours to Time in sql server.


For example :

IF workhours = 8 then the time equivalent value = 08:00:00

Upvotes: 0

Views: 125

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

Assuming your number of hours is always between 0 and 23, you can do this:

DECLARE @workhours int = 8
SELECT CAST(
            RIGHT('0'+ CAST(@workhours as varchar(2)), 2) -- pad with a leading '0'
            + ':00:00' -- add minutes and seconds
       As Time)

Upvotes: 1

Related Questions