一条SQL语句出错:服务器: 消息 8114,级别 16,状态 5,过程 JIEGUO33,行 14,将数据类型 varchar 转换为 float 时出错。

xuehan 2005-05-30 01:18:01
一条SQL语句出错:服务器:
消息 8114,级别 16,状态 5,过程 JIEGUO33,行 14
将数据类型 varchar 转换为 float 时出错。

存储过程:
CREATE PROCEDURE JIEGUO33
AS
select count(*) zcount,usernumber from sm_report where ISNUMERIC(usernumber)=1 and
cast(usernumber as float) in(select distinct usermobile from agent.dbo.smsorder
where isorder=1 and sp=0 and usermobile>0 )
group by usernumber order by usernumber
GO

其中
sm_report中字段usernumber为VARCHAR(11)
agent.dbo.smsorder中字段usermobile为FLOAT

请帮我看看是怎么回事,按道理我用ISNUMERIC(usernumber)=1已经排除不能转为FLOAT的值了,但还是说将数据类型 varchar 转换为 float 时出错
...全文
2904 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanduan1122 2005-06-01
  • 打赏
  • 举报
回复
解决了吧。
zjcxc 元老 2005-06-01
  • 打赏
  • 举报
回复
--改这个吧,应该是有数字型,但无法转换成float的数据。

select count(*) zcount,usernumber
from sm_report
where
case
when ISNUMERIC(usernumber)=1 and patindex('%[^0-9^.]%',usernumber)=0
then cast(usernumber as float) else null end
in(
select distinct usermobile from agent.dbo.smsorder
where isorder=1 and sp=0 and usermobile>0)
group by usernumber order by usernumber
zjcxc 元老 2005-06-01
  • 打赏
  • 举报
回复
给数据出来测试。
xuehan 2005-06-01
  • 打赏
  • 举报
回复
谢谢回答,zjcxc(邹建) ,用你的
select count(*) zcount,usernumber
from sm_report
where
case when ISNUMERIC(usernumber)=1 then cast(usernumber as float) else null end
in(
select distinct usermobile from agent.dbo.smsorder
where isorder=1 and sp=0 and usermobile>0)
group by usernumber order by usernumber
在查询分析器中执行还是一样的提示
zjcxc 元老 2005-05-30
  • 打赏
  • 举报
回复
关于这个问题,楼主将存储过程的语句复制到查询分析器,按Ctrl+L进行分析,就知道sql server的自动优化调整了条件顺序. 所以导致ISNUMERIC(usernumber)=1的条件后执行.
zjcxc 元老 2005-05-30
  • 打赏
  • 举报
回复
--sql server自动优化导致的,将语句改成下面的处理方式即可.

CREATE PROCEDURE JIEGUO33
AS
select count(*) zcount,usernumber
from sm_report
where
case when ISNUMERIC(usernumber)=1 then cast(usernumber as float) else null end
in(
select distinct usermobile from agent.dbo.smsorder
where isorder=1 and sp=0 and usermobile>0)
group by usernumber order by usernumber
GO
hsj20041004 2005-05-30
  • 打赏
  • 举报
回复
select * from tablename where usernumber like '%[^1-9]%'
hsj20041004 2005-05-30
  • 打赏
  • 举报
回复
你检查一下你的表中usernumber字段是否有字符或汉字什么的!!
Andy__Huang 2005-05-30
  • 打赏
  • 举报
回复
你做一下數據分析,看usernumber为VARCHAR(11)是否有數據不能轉換為float?

看這個錯誤提示,是數據轉換不了的。
good2speed 2005-05-30
  • 打赏
  • 举报
回复
ISNUMERIC只是确定表达式是否为一个有效的数字类型,
而并不表示可以安全的把这个表达式转成float


DECLARE @s varchar(10)

SET @s = '922,337,203,685,477.5807'

SELECT @s

SELECT ISNUMERIC (@s)

SELECT 1/CAST(@s AS float)

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧