简体   繁体   English

将小时转换为天:SQL Server中的小时格式

[英]Convert hours into days:Hours format in SQL Server

Create Table 
(
    id int identity(1,1),
    WorkHours int
)

This is my table which stores working hours as integer. 这是我的表,将工作时间存储为整数。 My requirement is to display work hours in DD:HH format. 我的要求是以DD:HH格式显示工作时间。 How is it possible? 这怎么可能?

To get it DD:HH format, you'll need to do some math as well as some 00 padding (or you get D:H) format: 要获得DD:HH格式,您需要做一些数学运算以及00填充(或获得D:H)格式:

select RIGHT('00' + CONVERT(VARCHAR, WorkHours / 24), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR, WorkHours% 24), 2)
from MyTable
with Calcs as
(
select id, floor(WorkHours/24) as DDField, (WorkHours/24)-floor(WorkHours/24) as HHField
from MyTable
)
select id, DDField + ':' + HHField
from Calcs
  • Integer division in SQL-Server is int1 / int2 . SQL Server中的整数除法是int1 / int2
  • Modulo in SQL-Server is int1 % int2 . SQL Server中的模数是int1 % int2
  • The function for formatting numbers in SQL-Server is FORMAT . 在SQL Server中格式化数字的功能是FORMAT
  • The string concatenation operator in SQL-Server is the non-standard + . SQL-Server中的字符串连接运算符是非标准+

The query: 查询:

select format(workhours / 24, '00') + ':' + format(workhours % 24, '00')
from mytable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM