简体   繁体   中英

Arithmetic overflow error converting varchar to data type numeric

Hi i have a query below which creates insert script for me. the column TotalPremiumValue has datatype Decimal(5,2) . when i execute my query i am getting the following error: Arithmetic overflow error converting varchar to data type numeric. can anybody help me?

SELECT 'IF NOT EXISTS(SELECT 1 FROM Lkup.TotalPremium WHERE [TotalPremiumValue]= '''+[TotalPremiumValue]+''') 
INSERT INTO Lkup.TotalPremium ' + 
 '(' + 
 '[TotalPremiumValue],' + 
 '[EffectiveDate]' + 
 ')' + 
 'VALUES (' +
 CASE WHEN [TotalPremiumValue] IS NULL THEN 'NULL' ELSE CONVERT(VARCHAR(40), [TotalPremiumValue]) END + ', ' +
 CASE WHEN [EffectiveDate] IS NULL THEN 'NULL' ELSE 'CONVERT(DATETIME, ' + master.sys.fn_varbintohexstr (CONVERT(BINARY(8), [EffectiveDate])) + ')' END + ', ' +
 ')'
 FROM Lkup.TotalPremium 

Thanks

Please try the below query

SELECT 
'IF  EXISTS(SELECT 1 FROM Lkup WHERE [TotalPremiumValue] = ' + CONVERT(varchar(50),     [TotalPremiumValue])  + ') 
INSERT INTO Lkup ' + 
'(' + 
'[TotalPremiumValue]' + 
')' + 
'VALUES (' + 
case when CONVERT(varchar(50),[TotalPremiumValue]) IS NULL then 'null' else CONVERT(varchar(50),TotalPremiumValue) end +
')' 
FROM  Lkup

Perhaps try recasting the result as a decimal(5,2)? I added the call into a portion of your code and pasted the section below. You could also shrink that varchar(40) down to a varchar(6), which would hold all 5 numbers plus the decimal point.

 'VALUES ( CAST(' + 
 CASE WHEN [TotalPremiumValue] IS NULL THEN 'NULL' ELSE CONVERT(VARCHAR(40), [TotalPremiumValue]) END + ' AS DECIMAL(5,2)), ' + 
 CASE WHEN [EffectiveDate] IS NULL THEN 'NULL' ELSE 'CONVERT(DATETIME, ' + master.sys.fn_varbintohexstr (CONVERT(BINARY(8), [EffectiveDate])) + ')' END + ', ' + 
 ')'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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