将Varchar转换为数字

时间:2013-09-05 07:17:25

标签: sql sql-server decimal numeric varchar

我需要将导入的VarChar转换为Text,并找到此代码

select * from [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] where ISNUMERIC(201307)=0
alter table [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] Add 201307_Temp decimal(17,4) 
GO

--Update temporary column with the values from 201307
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set 201307_Temp = 201307
GO

--set 201307 to null
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set 201307 = NULL
GO

--Change the datatype of 201307
alter table [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite]  alter column 201307         decimal(17,4) 
GO

--Update the numeric values in 201307
--before doing this you have make sure select * from [VWSA_Sewells].[dbo].    [Sewells_1Mth_Composite] where ISNUMERIC(201307_Temp)=0 returns nothing.
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set 201307 = Cast(201307_Temp as     decimal(17,4))
GO

我的表格在第1列包含Ratio_ID,例如VW1537_is02p,在第2列标题201307下包含实际Ratio

201307栏仅包含数字数据,例如0.2242,5042050.40等。这些数字都不超过4位小数。

我在SQL Server 2008中的错误信息是

  

Msg 102,Level 15,State 1,Line 3
  '201307'附近的语法不正确。

只要十进制提供最多10亿和4位的金额并允许查询(> 1等),我们就可以坚持使用十进制。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这应该有效:

select * from [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] where ISNUMERIC([201307])=0
alter table [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] Add [201307_Temp] decimal(17,4) 
GO
--Update temporary column with the values from 201307
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set [201307_Temp] = [201307]
GO
--set 201307 to null
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set [201307] = NULL
GO
--Change the datatype of 201307
alter table [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite]  alter column [201307] decimal(17,4) 
GO
--Update the numeric values in 201307
--before doing this you have make sure select * from [VWSA_Sewells].[dbo].    [Sewells_1Mth_Composite] where ISNUMERIC(201307_Temp)=0 returns nothing.
update [VWSA_Sewells].[dbo].[Sewells_1Mth_Composite] set [201307] = Cast([201307_Temp] as decimal(17,4))
GO

拉​​吉