开发者

How to Split Time and calculate time difference in sql server 2005?

开发者 https://www.devze.com 2023-04-11 22:35 出处:网络
i want to split the time and ca开发者_高级运维lculate time difference using sql server 2005 my default output is like this:

i want to split the time and ca开发者_高级运维lculate time difference using sql server 2005

my default output is like this:

EnrollNo     AttDateFirst                          AttDateLast
111      2011-12-09 08:46:00.000          2011-12-09 08:46:00.000
112      2011-12-09 08:40:00.000          2011-12-09 17:30:00.000
302      2011-12-09 09:00:00.000          2011-12-09 18:30:00.000
303      2011-12-09 10:00:00.000          2011-12-09 18:35:00.000

I want my new output to be like this:

Enroll No     .....      FirtTime       LastTime      Time Diff
111           .....      8:46:00          8:45:00     00:00:00
112           .....      8:30:00         17:30:00      9:00:00
302           .....      9:00:00         18:30:00      9:30:00 
303           .....     10:00:00         18:35:00      8:35:00


You can use this query:

select EnrollNo, convert(varchar, AttDateFirst, 8) as FirstTime,
                 convert(varchar, AttDateLast, 8) as LastTime,
                 convert(varchar, AttDateLast - AttDateFirst, 8) as [Time Diff]
from YourTable

to return the following results:

EnrollNo    FirstTime                      LastTime                       Time Diff
----------- ------------------------------ ------------------------------ ------------------------------
111         08:46:00                       08:46:00                       00:00:00
112         08:30:00                       17:30:00                       09:00:00
302         09:00:00                       18:30:00                       09:30:00
303         10:00:00                       18:35:00                       08:35:00


you can use

select DATEDIFF(day,2007-11-30,2007-11-20) AS NumberOfDays, DATEDIFF(hour,2007-11-30,2007-11-20) AS NumberOfHours, DATEDIFF(minute,2007-11-30,2007-11-20) AS NumberOfMinutes from test_table

to split u can use

substring(AttDateFirst,charindex(' ',AttDateFirst)+1 ,
                       len(AttDateFirst)) as [FirstTime]
0

精彩评论

暂无评论...
验证码 换一张
取 消