1

我需要将导入的文本转换VarChar为文本,并找到了这段代码

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例如,在标题 201307 下的第 2列中包含VW1537_is02p实际值。Ratio

第 201307 列仅包含数字数据,例如 0.2242、5042050.40 等。所有数字都没有超过 4 位小数。

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

消息 102,级别 15,状态 1,第 3 行
“201307”附近的语法不正确。

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

有任何想法吗?

4

1 回答 1

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

拉吉

于 2013-09-05T07:31:45.113 回答