Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert second into datetime's 108 format hh:mm:ss in sql server without writing function

Tags:

sql

sql-server

    select v1.*, datediff(ss,v1.dateofchange,v2.dateofchange) as acutaltime 
from vActualTime v1 left join vActualTime v2
    on v1.rowno=v2.rowno-1 

FK_PatientId FK_Status_PatientId DateofChange            rowno                acutaltime
------------ ------------------- ----------------------- -------------------- -----------
3            16                  2010-08-02 15:43:46.000 1                    757
3            24                  2010-08-02 15:56:23.000 2                    96
3            26                  2010-08-02 15:57:59.000 3                    NULL

I am using Sql server 2005

When I writes this

    select v1.*, datediff(mi,v1.dateofchange,v2.dateofchange) as acutaltime,
    convert(datetime,datediff(mi,v1.dateofchange,v2.dateofchange),108) as [date] 
from vActualTime v1 left join vActualTime v2
    on v1.rowno=v2.rowno-1 

I gets this

FK_PatientId FK_Status_PatientId DateofChange            rowno                acutaltime  date
------------ ------------------- ----------------------- -------------------- ----------- -----------------------
3            16                  2010-08-02 15:43:46.000 1                    13          1900-01-14 00:00:00.000
3            24                  2010-08-02 15:56:23.000 2                    1           1900-01-02 00:00:00.000
3            26                  2010-08-02 15:57:59.000 3                    NULL        NULL

This should have been given as 00-00-000 00:13:00:0000

like image 348
Shantanu Gupta Avatar asked Dec 31 '25 22:12

Shantanu Gupta


1 Answers

From what I understand you need to take your calculated minutes (the datediff you're doing) and display that in a time format 108.

This should convert the minutes to a datetime format of 108, i.e. hh:mm:ss

select convert(varchar
              ,dateadd(minute
                      , datediff(mi,v1.dateofchange,v2.dateofchange), '00:00:00')
              , 108
              )
like image 125
Kamal Avatar answered Jan 03 '26 12:01

Kamal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!