How can convert decimal value into time format as its

Polachan Paily 226 Reputation points
2021-02-02T16:11:12.91+00:00

How can change to decimal values in to time format as it is without any change in values

declare @intime decimal (10,2) = 7.50
declare @intime1 decimal (10,2) = 17.50
declare @breakhrs decimal (10,2) = .45
declare @breakhrs decimal (10,2) = 1.00

I want to change the above decimal as given below

7.50 as 7:50:00
17.50 as 17:50:00
.45 as 00:45:00
1.00 as 01:00:00

How can I convert please help

Developer technologies Transact-SQL
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-02-03T03:31:58.823+00:00

    Hi @Polachan Paily ,

    Please refer to:

    create table #time (intime decimal (10,2))  
    insert into #time values(7.5),(17.50),(.45),(1.00)  
          
    select intime,cast(replace(intime,'.',':') as time(0)) [Time]  
    from #time  
    

    Output:

        intime [Time]  
        7.50 07:50:00  
        17.50 17:50:00  
        0.45 00:45:00  
        1.00 01:00:00  
    

    Date and time conversion can use CAST and CONVERT (Transact-SQL).The time (Transact-SQL) keyword in TSQL can define different forms of time.

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-02-02T16:35:05.027+00:00
    DECLARE @time TABLE (intime decimal (10,2))
    
    INSERT INTO @time VALUES
    (7.5),
    (17.50),
    (.45),
    (1.00)
    
    
    SELECT intime,
        TRY_CAST(REPLACE(intime,'.',':') + ':00' AS time(0)) as intime_Time
    FROM @time
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.