无法将varchar转换为sql中的float

时间:2013-03-08 11:19:30

标签: sql sql-server sql-server-2008 sql-server-2005

这是我的2张桌子

CREATE TABLE [dbo].[dailyRate](
[SYMBOL] [varchar](50) NULL,
[SERIES] [varchar](50) NULL,
[OPENPRICE] [varchar](50) NULL,
[HIGHPRICE] [varchar](50) NULL,
[LOWPRICE] [varchar](50) NULL,
[CLOSEPRICE] [varchar](50) NULL,
[LASTPRICE] [varchar](50) NULL,
[PREVCLOSE] [varchar](50) NULL,
[TOTTRDQTY] [varchar](50) NULL,
[TOTTRDVAL] [varchar](50) NULL,
[TIMESTAMPDAY] [varchar](50) NULL,
[TOTALTRADES] [varchar](50) NULL,
[ISIN] [varchar](50) NULL
)


CREATE TABLE [dbo].[cmpDailyRate](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[SYMBOL] [varchar](50) NULL,
[SERIES] [varchar](50) NULL,
[OPENPRICE] [decimal](18, 4) NULL,
[HIGHPRICE] [decimal](18, 4) NULL,
[LOWPRICE] [decimal](18, 4) NULL,
[CLOSEPRICE] [decimal](18, 4) NULL,
[LASTPRICE] [decimal](18, 4) NULL,
[PREVCLOSE] [decimal](18, 4) NULL,
[TOTTRDQTY] [bigint] NULL,
[TOTTRDVAL] [decimal](18, 4) NULL,
[TIMESTAMPDAY] [smalldatetime] NULL,
[TOTALTRADES] [bigint] NULL,
[ISIN] [varchar](50) NULL,
[M_Avg] [decimal](18, 4) NULL
) 

这是我的插入查询,用于使用强制转换

从表中获取数据

折叠|复制代码

INSERT into [Stock].[dbo].[cmpDailyRate]
SELECT [SYMBOL],[SERIES],Str([OPENPRICE], 18,4),Str([HIGHPRICE],18,4),
Str([LOWPRICE],18,4),Str([CLOSEPRICE],18,4),Str([LASTPRICE],18,4),Str([PREVCLOSE],18,4),convert(bigint,[TOTTRDQTY]),Str([TOTTRDVAL],18,4),
convert(date, [TIMESTAMPDAY], 105),convert(bigint,[TOTALTRADES]),[ISIN],null
FROM [Stock].[dbo].[DailyRate]

此查询在SQL Server 2005中运行完美,但它在SQL Server 2008中导致错误(上面的查询在安装时也在SQL Server 2008中运行;错误在最近几天出现)

错误:

  

错误无法将varchar转换为float

怎么办?

2 个答案:

答案 0 :(得分:1)

您的一行包含正在执行浮点转换(Str)的列中的无效数据。使用以下策略来确定哪个:

SELECT *
FROM [dailyRate]
WHERE IsNumeric([OPENPRICE]) = 0
OR IsNumeric([HIGHPRICE]) = 0

等等。

答案 1 :(得分:0)

如果您不想过滤数据,CASE语句可能会更适合您。

SELECT CASE 
        WHEN IsNumeric([OPENPRICE]) = 1 THEN [OPENPRICE]
        ELSE NULL -- or 0 or whatever
       END AS OPENPRICE,
       CASE 
        WHEN IsNumeric([HIGHPRICE]) = 1 THEN [HIGHPRICE]
        ELSE NULL -- or 0 or whatever
       END AS [HIGHPRICE]
FROM [dailyRate]