将数据类型nvarchar转换为小数位

时间:2017-06-08 14:49:01

标签: sql sql-server tsql

我正在为我的客户建立一份工作成本核算报告。他们的会计科目表包含十分位数到第十位(即1001.55)。我遇到了一个问题,无论我做什么,我都无法将nvarchar转换为浮动,整数或数字。如果我拿出最后一个Where子句,我试图通过COA 5310过滤,那么它运行没有问题。我忽略了一个明显的明显问题?任何帮助将不胜感激!

--Build Equiptment
Create Table tmpEquiptment
(
       [dtmDate] datetime
       ,[iCustID] int
       ,[Amount] money
       ,[COA_Number]  DECIMAL(18,2)
)
Insert into tmpEquiptment
       SELECT dtmDate, iCustID, (Register.Debit - Register.Credit), STR(COA.Number)
       From Register 
       Inner Join COA on Register.AccountID = COA.ID  
       WHERE TranType IN ('Payment', 'Invoice', 'Sale', 'Credit', 'Journal Entry', 'Check', 'Item Receipt', 'Bill', 'Vendor Credit', 'CC Charge', 'CC Credit', 'Deposit', 'Inventory Adjustment', 'Serialized Adjustment') 
       AND Coa.Type in ('Cost of Goods Sold','Expense','Other Expense') 
       AND Isnull(COA.iJobCostingType,0) In (2,3)  
       AND Register.iCustID Is Not Null
       And Register.dtmDate Between '2016-01-01 00:00:00.000' AND '2018-01-01 00:00:00.000'
       And STR(COA.Number) = 5310

- 或 -

--Build Equiptment
Create Table tmpEquiptment
(
       [dtmDate] datetime
       ,[iCustID] int
       ,[Amount] money
       ,[COA_Number]  DECIMAL(18,2)
)
Insert into tmpEquiptment
       SELECT dtmDate, iCustID, (Register.Debit - Register.Credit), CONVERT(varchar(10), COA.Number)
       From Register 
       Inner Join COA on Register.AccountID = COA.ID  
       WHERE TranType IN ('Payment', 'Invoice', 'Sale', 'Credit', 'Journal Entry', 'Check', 'Item Receipt', 'Bill', 'Vendor Credit', 'CC Charge', 'CC Credit', 'Deposit', 'Inventory Adjustment', 'Serialized Adjustment') 
       AND Coa.Type in ('Cost of Goods Sold','Expense','Other Expense') 
       AND Isnull(COA.iJobCostingType,0) In (2,3)  
       AND Register.iCustID Is Not Null
       And Register.dtmDate Between '2016-01-01 00:00:00.000' AND '2018-01-01 00:00:00.000'
       And CONVERT(varchar(10), COA.Number) = 5310

- 或 -

--Build Equiptment
Create Table tmpEquiptment
(
       [dtmDate] datetime
       ,[iCustID] int
       ,[Amount] money
       ,[COA_Number]  DECIMAL(18,2)
)
Insert into tmpEquiptment
       SELECT dtmDate, iCustID, (Register.Debit - Register.Credit), CONVERT(DECIMAL(18,2), COA.Number)
       From Register 
       Inner Join COA on Register.AccountID = COA.ID  
       WHERE TranType IN ('Payment', 'Invoice', 'Sale', 'Credit', 'Journal Entry', 'Check', 'Item Receipt', 'Bill', 'Vendor Credit', 'CC Charge', 'CC Credit', 'Deposit', 'Inventory Adjustment', 'Serialized Adjustment') 
       AND Coa.Type in ('Cost of Goods Sold','Expense','Other Expense') 
       AND Isnull(COA.iJobCostingType,0) In (2,3)  
       AND Register.iCustID Is Not Null
       And Register.dtmDate Between '2016-01-01 00:00:00.000' AND '2018-01-01 00:00:00.000'
       And CONVERT(DECIMAL(18,2), COA.Number) = 5310

- 或 -

--Build Equiptment
Create Table tmpEquiptment
(
       [dtmDate] datetime
       ,[iCustID] int
       ,[Amount] money
       ,[COA_Number]  DECIMAL(18,2)
)
Insert into tmpEquiptment
       SELECT dtmDate, iCustID, (Register.Debit - Register.Credit), CAST([COA.Number] AS NVARCHAR(25))
       From Register 
       Inner Join COA on Register.AccountID = COA.ID  
       WHERE TranType IN ('Payment', 'Invoice', 'Sale', 'Credit', 'Journal Entry', 'Check', 'Item Receipt', 'Bill', 'Vendor Credit', 'CC Charge', 'CC Credit', 'Deposit', 'Inventory Adjustment', 'Serialized Adjustment') 
       AND Coa.Type in ('Cost of Goods Sold','Expense','Other Expense') 
       AND Isnull(COA.iJobCostingType,0) In (2,3)  
       AND Register.iCustID Is Not Null
       And Register.dtmDate Between '2016-01-01 00:00:00.000' AND '2018-01-01 00:00:00.000'
       And CAST([COA.Number] AS NVARCHAR(25)) = 5310

2 个答案:

答案 0 :(得分:1)

感谢@Jacob H对bjones回答的解释。这是一个简单过度设计解决方案的问题,当它真的只是一个ID10T错误时。对于String值(带单引号)

,不能使用int(无引号读)
--Build Equiptment
Create Table tmpEquiptment
(
       [dtmDate] datetime
       ,[iCustID] int
       ,[Amount] money
       ,[COA_Number]  DECIMAL(18,2)
)
Insert into tmpEquiptment
       SELECT dtmDate, iCustID, (Register.Debit - Register.Credit), COA.Number
       From Register 
       Inner Join COA on Register.AccountID = COA.ID  
       WHERE TranType IN ('Payment', 'Invoice', 'Sale', 'Credit', 'Journal Entry', 'Check', 'Item Receipt', 'Bill', 'Vendor Credit', 'CC Charge', 'CC Credit', 'Deposit', 'Inventory Adjustment', 'Serialized Adjustment') 
       AND Coa.Type in ('Cost of Goods Sold','Expense','Other Expense') 
       AND Isnull(COA.iJobCostingType,0) In (2,3)  
       AND Register.iCustID Is Not Null
       And Register.dtmDate Between '2016-01-01 00:00:00.000' AND '2018-01-01 00:00:00.000'
       And COA.Number = '5310'

答案 1 :(得分:0)

您的问题可能会发生,因为COA.Number中行的某些不是数字。您可以使用以下查询进行验证:

select * from COA where ISNUMERIC(COA.Number) <> 1

现在您可以修复这些行,也可以在创建新表时将其过滤掉:

WHERE ISNUMERIC(COA.Number) = 1