22,210
社区成员
发帖
与我相关
我的任务
分享
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar
)
go
insert into t
select 'a' union all
select '1'
go
select * from (
select * from t where ISNUMERIC(id)=1
)a
/*
id
----
1
*/
go
select * from (
select * from t where ISNUMERIC(id)=1
)a where id>1
/*
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'a' to data type int.
*/
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar(10)
)
go
insert into t
select 'a' union all
select '10'
go
select * from (
select * from t where ISNUMERIC(id)=1
)a
/*
id
----
10
*/
go
------------------------------------------------解决方案
select * from (
select
case when ISNUMERIC(id)=1
then CONVERT(numeric,id)
else CONVERT(numeric,0)
end as id
from t where ISNUMERIC(id)=1
)a where id>1
/*
id
----
10
*/
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar
)
go
insert into t
select 'a' union all
select '1'
go
select * from (
select * from t where ISNUMERIC(id)=1
)a
/*
id
----
1
*/
go
select * from (
select * from t where ISNUMERIC(id)=1
)a where ISNUMERIC(id)>=1
/*
id
1
*/
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar(10)
)
go
insert into t
select 'a' union all
select '10'
go
select * from (
select * from t where ISNUMERIC(id)=1
)a
/*
id
----
10
*/
go
select * from (
select * from t where ISNUMERIC(id)=1
)a where id>1
/*
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'a' to data type int.
*/
select * into #t from t where ISNUMERIC(id)=1 and id>4
use CSDN
go
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar(100)
)
go
insert into t
select '$1' union all --注意isnumeric函数的弊端
select '1' union all
select 'a'
go
--参考如下:
select right(replicate('0', 10)+id, 10) from t --int类型不会超过10位
where patindex('%[^0-9]%', id) = 0
and id > replicate('0', 9) + '1'
if OBJECT_ID('t','U') is not null drop table t
go
create table t
(
id nvarchar
)
go
insert into t
select 'a' union all
select '1' union all
select '5' union all
select '8'
go
select * into #t from t where ISNUMERIC(id)=1
select * from #t where id>4
/*
id
----
5
8
*/
drop table #t
--插入临时表是可以的,但是用with表达式就不行...