34,571
社区成员
发帖
与我相关
我的任务
分享
最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
如果有多表,表之间如何关联?
ISNUMERIC
确定表达式是否为一个有效的数字类型。
语法
ISNUMERIC ( expression )
参数
expression
要计算的表达式。
返回类型
int
注释
当输入表达式得数为一个有效的整数、浮点数、money 或 decimal 类型,那么 ISNUMERIC 返回 1;否则返回 0。返回值为 1 确保可以将 expression 转换为上述数字类型中的一种。
示例
A. 使用 ISNUMERIC
下面的示例返回 1,这是因为 zip 列包含有效的数值。
USE pubs
SELECT ISNUMERIC(zip)
FROM authors
GO
B. 使用 ISNUMERIC 和 SUBSTRING
下面的示例对于 titles 表中的所有书名都返回 0,这是因为没有一个书名是有效的数值。
USE pubs
GO
-- Because the title column is all character data, expect a result of 0
-- for the ISNUMERIC function.
SELECT SUBSTRING(title, 1, 15) type, price, ISNUMERIC(title)
FROM titles
GO
下面是结果集:
type price
--------------- -------------------------- -----------
The Busy Execut 19.99 0
Cooking with Co 11.95 0
You Can Combat 2.99 0
Straight Talk A 19.99 0
Silicon Valley 19.99 0
The Gourmet Mic 2.99 0
The Psychology (null) 0
But Is It User 22.95 0
Secrets of Sili 20.00 0
Net Etiquette (null) 0
Computer Phobic 21.59 0
Is Anger the En 10.95 0
Life Without Fe 7.00 0
Prolonged Data 19.99 0
Emotional Secur 7.99 0
Onions, Leeks, 20.95 0
Fifty Years in 11.95 0
Sushi, Anyone? 14.99 0
(18 row(s) affected)
declare @homecode varchar(20) ='123456'
declare @humancode varchar(20) ='456789'
declare @villagecode varchar(20)
set @villagecode = 'A0000001'
declare @Archives_CM table(id int)
insert into @Archives_CM
select 1
update @Archives_CM set id =
CAST(right(@villagecode,4) as int)+
cast(@humancode as int) +
cast(@homecode as int)
select * from @Archives_CM
-- 查出不可转换的
select * from dbo.Archives_CM where (right(villagecode,4)+homecode) like '%[^0-9]%'
-- 更新可转换的
update dbo.Archives_CM set id=cast((right(villagecode,4)+homecode) as bigint)
where (right(villagecode,4)+homecode) not like '%[^0-9]%'