小编use*_*840的帖子

SQL Server:将数据类型varchar转换为numeric时出错

我有一张桌子:

Account_Code | Desc
503100       | account xxx
503103       | account xxx
503104       | account xxx
503102A      | account xxx
503110B      | account xxx
Run Code Online (Sandbox Code Playgroud)

哪里Account_Codevarchar.

当我在下面创建查询时:

Select 
  cast(account_code as numeric(20,0)) as account_code,
  descr 
from account 
where isnumeric(account_code) = 1
Run Code Online (Sandbox Code Playgroud)

它通过返回account_code列中具有有效数值的所有记录运行良好.

但是当我尝试添加另一个select时,嵌套到先前的sql:

select account_code,descr 
from 
(
  Select cast(account_code as numeric(20, 0)) as account_code,descr 
  from account 
  where isnumeric(account_code) = 1
) a 
WHERE account_code between 503100 and 503105
Run Code Online (Sandbox Code Playgroud)

查询将返回错误

将数据类型varchar转换为数字时出错.

那里发生了什么?

如果account_code有效,我已经转换为数字,但似乎查询仍在尝试处理无效的记录.

我需要BETWEEN …

sql-server varchar numeric

21
推荐指数
3
解决办法
22万
查看次数

标签 统计

numeric ×1

sql-server ×1

varchar ×1