我有一个SQL函数,它从多个记录中收集人工时间,并使用STUFF(CAST AS VARCHAR(20))
将它们连接到临时表中的单个列。
我的问题是我的表中的人工值是以小时为单位并向下舍入到7位小数,我想将这些值转换为分钟并将它们舍入到2位小数,然后将它们填充到临时表中。
更新:忘记提及,我正在使用SQL Server 2008 R2
这是我的代码。
@site nvarchar(20)
,@item nvarchar(30)
,@enviro nvarchar(30))
RETURNS nvarchar(MAX)
AS
BEGIN
DECLARE @LbrHours AS nvarchar(MAX) = ''
IF @enviro = 'Test'
BEGIN
IF @site = 'Arborg'
BEGIN
SET @LbrHours = STUFF((SELECT ',' + CAST(jrt.run_lbr_hrs AS VARCHAR(20))
FROM Arborg_Test_App.dbo.jobroute AS jbr
INNER JOIN Arborg_Test_App.dbo.job AS job
ON job.job = jbr.job
AND job.suffix = jbr.suffix
INNER JOIN Arborg_Test_App.dbo.item AS itm
ON itm.job = job.job
INNER JOIN Arborg_Test_App.dbo.jrt_sch AS jsh
ON jbr.job = jsh.job
AND jbr.suffix = jsh.suffix
AND jbr.oper_num = jsh.oper_num
LEFT OUTER JOIN Arborg_Test_App.dbo.jrt_sch AS jrt
ON jbr.job = jrt.job
AND jbr.suffix = jrt.suffix
AND jbr.oper_num = jrt.oper_num
WHERE job.suffix = '0' and job.type = 'S' AND itm.item IS NOT NULL
AND itm.item = @item
AND jbr.suffix = CASE -- Return Standard cost if Standard Operation exist, else return current cost
WHEN itm.cost_type = 'S'
THEN '1' -- '1' for standard operation
ELSE '0' -- '0' for current operations
END
ORDER BY itm.item, jbr.oper_num
FOR XML PATH('')), 1, 1, '')
END
END
RETURN @LbrHours
END
jrt.run_lbr_hrs
是包含ERP表格中以小时为单位的人工时间的列。如何在现有的STUFF(CASE AS NVARCHAR)
中将其乘以60并将其四舍五入为2位小数?
答案 0 :(得分:2)
CAST
到十进制,在点之后有2个值,然后到varchar
CAST(CAST(jrt.run_lbr_hrs * 60 AS DECIMAL(10, 2)) AS VARCHAR(20))
将小数尺寸更改为您需要的尺寸
答案 1 :(得分:0)
试试这个Str (jrt.run_lbr_hrs * 60, 2)