如何将char类型数据转化为int

nancliu 2007-02-08 12:57:57
如何将char类型数据转化为int?
这样转化后数据库的查询效率会不会高些?
...全文
257 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nancliu 2007-02-08
  • 打赏
  • 举报
回复
总分不可更改了,抱歉
多谢大家
nancliu 2007-02-08
  • 打赏
  • 举报
回复
收到,结帖
rookie_one 2007-02-08
  • 打赏
  • 举报
回复
先判断一下这列是不是都是数字型
select count(*) from [tablename] where isnumeric([ID])=0
如果结果为0那就可以运行下边这个直接转化列数据类型

alter table [tablename] alter column [colname] int
rookie_one 2007-02-08
  • 打赏
  • 举报
回复
---
rookie_one 说的那个判断不知道怎么加上去,我要转化一列的数据
--

-----------------
select
case when isnumeric([ID])=1 then cast([ID] as int) else [ID] end into newtable
from oldtable
nancliu 2007-02-08
  • 打赏
  • 举报
回复
20不够大家分的,晚上加分,我会多给些分的
nancliu 2007-02-08
  • 打赏
  • 举报
回复
明白了,我如果把ID列转化为int,
select cast(ID as int) into newtable from oldtable
OR
select convert(int,ID) into newtable from oldtable
---
rookie_one 说的那个判断不知道怎么加上去,我要转化一列的数据
--
另外转换后查询效率会不会高些呢,数据很多,电脑配置又低..
rookie_one 2007-02-08
  • 打赏
  • 举报
回复
declare @a char(10)
set @a='345'
if isnumeric(@a)=1
begin
print cast(@a as int)
end
else
print @a+'不是数字型'


--------
345
rookie_one 2007-02-08
  • 打赏
  • 举报
回复
先用isnumeric判断是否是纯数字

declare @a char(10)
set @a='2a'--这里是将转化的字符
if isnumeric(@a)=1
begin
print cast(@a as int)
end
else
print @a+'不是数字型'

---
2a 不是数字型
OracleRoob 2007-02-08
  • 打赏
  • 举报
回复
cast('123' as int)

--or

convert(int,'123')
feiyun0112 2007-02-08
  • 打赏
  • 举报
回复
cast (field as int)

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
chuifengde 2007-02-08
  • 打赏
  • 举报
回复
cast('234' as int)

34,590

社区成员

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

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