nvarchar 和 int 比较

bean_sql 2012-06-13 12:19:03



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.
*/
...全文
244 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bean_sql 2012-06-13
  • 打赏
  • 举报
回复
感谢你的答复,
解决方案如下:



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
*/


[Quote=引用 2 楼 的回复:]

SQL code

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 ISNUMERI……
[/Quote]
Felixzhaowenzhong 2012-06-13
  • 打赏
  • 举报
回复
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
*/
bean_sql 2012-06-13
  • 打赏
  • 举报
回复

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.
*/
發糞塗牆 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
SQL code



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 ISNUMERI……
[/Quote]
在最后的where id>1那里改成where ISNUMERIC(id)>1
黄_瓜 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

微软想帮你优化引出的问题,原谅他吧,知道有这个问题就可以了
[/Quote]
对,他把语句优化为

select * into #t from t where ISNUMERIC(id)=1 and  id>4

同时筛选 而不是先里面后外面。
SQL777 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
微软想帮你优化引出的问题,原谅他吧,知道有这个问题就可以了
[/Quote]
嘿嘿。人总有放错的时候。何况是软件。海爷说的是
昵称被占用了 2012-06-13
  • 打赏
  • 举报
回复
微软想帮你优化引出的问题,原谅他吧,知道有这个问题就可以了
Shawn 2012-06-13
  • 打赏
  • 举报
回复
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'
叶子 2012-06-13
  • 打赏
  • 举报
回复

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表达式就不行...
--小F-- 2012-06-13
  • 打赏
  • 举报
回复
以前海爷讨论过这个。温习一下。
从一个mysql数据库导过来的,算是挺新的数据库吧。 把sql语句里第一句的USE [HotelManager] GO中HotelManager改成你建立的数据库名。。。 T_province表 省 自治区 直辖市(一级) [serialId] [bigint] NOT NULL Id [provinceId] [nvarchar] NULL 省id [provinceName] [nvarchar] NULL 省名 [provinceUpId] [nvarchar] NULL 上一级id [provinceUpIdNum] [int] NULL 上一级名字 [provincePath] [nvarchar] NULL 省路径 [provinceType] [nvarchar] NULL 类型 [provinceTypeNum] [int] NULL 类型等级 [shortName] [nvarchar] NULL 简称(汉语拼音) [spell] [nvarchar] NULL 拼音拼写 [areaId] [nvarchar] NULL 区域id [postCode] [nvarchar] NULL 邮政编码 T_city表 市 县 区 地区 州(二级) serialId bigint NOT NULL Id cityId nvarchar NULL 市id cityName nvarchar NULL 市名 cityUpId nvarchar NULL 上一级id cityUpIdNum nvarchar NULL 上一级名字 cityPath nvarchar NULL 完整路径 cityType nvarchar NULL 市类型 cityTypeNum bigint NULL 类型等级 shortName nvarchar NULL 简称(汉语拼音) spell nvarchar NULL 拼音拼写 areaId nvarchar NULL 区域id postCode nvarchar NULL 邮政编码 T_district 区域 (三级) serialId bigint NOT NULL Id districtId nvarchar NULL 区域id districtName nvarchar NULL 区域名 districtUpId nvarchar NULL 上一级id districtUpIdNum int NULL 上一级名字 districtPath nvarchar NULL 完整路径 districtType nvarchar NULL 类型 districtTypeNum int NULL 类型等级 shortName nvarchar NULL 简称(汉语拼音) spell nvarchar NULL 拼音拼写 areaId nvarchar NULL 区域id postCode nvarchar NULL 邮政编码

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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