我有两个字段将数据保存为varchar
field1 = 10,000.00 and field2 = 5,000.00
我现在需要添加这两个字段来查找总和。但当我添加为Field1 + Field2时
结果是10,000.005,000.oo
我尝试使用CAST(Field1作为十进制(12,2)),但它抛出一个错误,它无法将varchar转换为数字。
帮帮我。
SELECT [1] AS ContractPriceExcluded, [2] AS Anticipated, [3] AS Approved, [4] AS claims, [5] AS ProvisionalSums, [6] AS CommitmentsToDate, [7] AS AnticipatedFCost,
[8] AS VarianceToContract, [9] AS CertifiedPayments,[10] AS ContractPriceIncluded , [11] AS ProvisionalSumsNotactivated ,description, status, ReferenceNo
FROM (SELECT ISNULL([Workflow_CustomFormfields].[fieldnumber], '') AS fieldnumber,
CAST( REPLACE([Workflow_CustomFormFields].fieldvalue,',','')as Decimal(12,2)) AS fieldvalue, isnull(workflow_customforms.subject, '') AS description,
isnull(workflow_customforms.categoryvalue, '') AS status, isnull(workflow_customforms.reference, '') AS ReferenceNo
FROM [dbo].[Workflow_CustomFormfields] INNER JOIN
dbo.Workflow_CustomForms ON [dbo].[Workflow_CustomForms].[Id] = dbo.Workflow_CustomFormfields.customformId
WHERE dbo.Workflow_CustomForms.ProjectId = 1 AND dbo.Workflow_CustomForms.CustomformtypeId = 75) PS PIVOT (max(fieldvalue) FOR fieldnumber IN ([1], [2],
[3], [4], [5], [6], [7], [8], [9],[10],[11])) AS pivot_fieldvalue
答案 0 :(得分:4)
select cast(replace('10,000.00',',','') as decimal(12,2))
问题在于逗号
答案 1 :(得分:0)
您是否将这两个值都转换为十进制?这很好用
DECLARE @one varchar(8), @two varchar(8)
SET @one = '10,000.00'
SET @two = '5,000.00'
SELECT CAST(replace(@one,',','') AS decimal(12,2))+
CAST(replace(@two,',','') AS decimal(12,2))
答案 2 :(得分:0)
您必须删除逗号并转换为十进制。见下文。
declare @field1 varchar(50) = '10,000.00' ,
@field2 varchar(50)= '5,000.00'
select Convert(decimal(10,2),replace(@field1,',','')) +Convert(decimal(10,2),replace(@field2,',',''))