您的位置:程序门 -> ms-sql server -> 基础类



我想将varchar转换成numeric,如果转换不了就用0替换。请问sql语句怎样写?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


我想将varchar转换成numeric,如果转换不了就用0替换。请问sql语句怎样写?[已结贴,结贴人:penny4182]
发表于:2008-02-23 13:54:57 楼主
a字段   类型是varchar(30)

我想将a字段的值的类型转换成numeric,如果转换不了就用0替换。请问sql语句怎样写?

原来的值(varchar)           转换后的值(numeric)
-----------------------------
1234                                       1234

5678                                       5678

ffff                                         0

f456                                         0

9876                                       9876

发表于:2008-02-23 13:55:231楼 得分:0
发表于:2008-02-23 13:58:152楼 得分:0
case  
发表于:2008-02-23 13:58:313楼 得分:10
使用
sql code
select 原来的值, case when isnumeric(原来的值)=1 then cast(原来的值 as varchar) else '0' end as 转换后的值 from tb
发表于:2008-02-23 14:07:194楼 得分:10
sql code
declare @t table(a varchar10)) insert into @t select '1234' union all select '5678' union all select 'ffff' union all select 'f456' union all select '9876' select * from @t select case when a like '%[a-z]%' then 0.0 else convert(numeric(9,4),a) end from @t
发表于:2008-02-23 14:08:405楼 得分:0
学习ing...
发表于:2008-02-23 14:12:256楼 得分:0
sql code
declare @t table(a varchar10)) insert into @t select '1234' union all select '5678' union all select 'ffff' union all select 'f456' union all select '9876' select * from @t select a as '原数据' ,'转换后数据'= case when isnumeric(a)=1 then convert(numeric(9,4),a) else 0.0 end from @t /* (所影响的行数为 5 行) a ---------- 1234 5678 ffff f456 9876 (所影响的行数为 5 行) 原数据 转换后数据 ---------- ----------- 1234 1234.0000 5678 5678.0000 ffff .0000 f456 .0000 9876 9876.0000 (所影响的行数为 5 行) */
发表于:2008-02-23 14:30:517楼 得分:0
agree
发表于:2008-02-23 15:29:098楼 得分:0
我想实现这样一个功能,
判断一个varchar变量,如果变量可以转换成numeric的就显示numeric,如果不能,就显示numeric。
发表于:2008-02-23 15:29:349楼 得分:0
我想实现这样一个功能,  
判断一个varchar变量,如果变量可以转换成numeric的就显示numeric,如果不能,就显示varchar。
发表于:2008-02-23 22:01:0710楼 得分:0
sql code
select 原来的值, case when isnumeric(原来的值)=1 then cast(原来的值 as varchar) else 原来的值 end as 转换后的值 from t
发表于:2008-02-24 17:57:1011楼 得分:0
用case  

sql code
select case when a like '%[a-z]%' then 0.0 else convert(numeric(9,4),a) end from @t


或者   case   isnumeric(a)  
                    when   1   then   convert(numeric(9,4),a)
                    else   0   end
发表于:2008-02-24 18:04:1412楼 得分:0
a字段       类型是varchar(30)  

我想将a字段的值的类型转换成numeric,如果转换不了就用0替换。请问sql语句怎样写?  

原来的值(varchar)                       转换后的值(numeric)  
-----------------------------  
1234                                                                               1234  

5678                                                                               5678  

ffff                                                                                   0  

f456                                                                                   0  

9876                                                                               9876  
----------------------------

sql code
--方法一:select 字段 = cast(字段 as bigint) from tb where isnumeric(字段) = 1 union all select 字段 = 0 from tb where isnumeric(字段) <> 1


sql code
--方法二:select 字段 = case when isnumeric(字段) = 1 then cast(字段 as bigint) else 0 end from tb
发表于:2008-02-24 18:08:4313楼 得分:0
sql code
create table tb(字段 varchar10)) insert into tb values'1234') insert into tb values'5678') insert into tb values'ffff') insert into tb values'f456') insert into tb values'9876') go select 字段1 = 字段, 字段2 = cast(字段 as bigint) from tb where isnumeric(字段) = 1 union all select 字段1 = 字段, 字段2 = 0 from tb where isnumeric(字段) <> 1 /* 字段1 字段2 ---------- -------------------- 1234 1234 5678 5678 9876 9876 ffff 0 f456 0 (所影响的行数为 5 行) */ select 字段1 = 字段, 字段2 = case when isnumeric(字段) = 1 then cast(字段 as bigint) else 0 end from tb /* 字段1 字段2 ---------- -------------------- 1234 1234 5678 5678 ffff 0 f456 0 9876 9876 (所影响的行数为 5 行) */ drop table tb
发表于:2008-02-24 18:26:1714楼 得分:0
sql code
declare @t table(a varchar10)) insert into @t select '1234' union all select '5678' union all select 'ffff' union all select 'f456' union all select '9876' select a=case when patindex'%[^0-9.]%',a)>0 then 0 else cast(a as decimal18,0)) end from @t /* (所影响的行数为 5 行) a -------------------- 1234 5678 0 0 9876 (所影响的行数为 5 行) */
发表于:2008-02-25 00:35:1815楼 得分:0
sql code
declare @t table(a varchar10)) insert into @t select '1234' union all select '5678' union all select 'ffff' union all select 'f456' union all select '9876' select * from @t select case isnumeric(a) when 1 then convertint,a) else 0 end from @t


快速检索

最新资讯
热门点击