0

这是我的两张桌子

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

该怎么办?

4

2 回答 2

1

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

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

等等等等

于 2013-03-08T11:30:31.403 回答
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]
于 2013-03-08T14:19:12.840 回答