[英]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
int1 / int2
. SQL Server中的整数除法是int1 / int2
。 int1 % int2
. SQL Server中的模数是int1 % int2
。 FORMAT
. 在SQL Server中格式化数字的功能是FORMAT
。 +
. SQL-Server中的字符串连接运算符是非标准+
。 The query: 查询:
select format(workhours / 24, '00') + ':' + format(workhours % 24, '00')
from mytable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.