如何取得字段中的数字??菜鸟发问!
大家好!
我想在某个字段(varchar类型)的内容中取出数字,请问大虾们如何处理阿?
比如:
字段:column1
类型:varchar(255)
内容:我要送给你1001朵玫瑰
我现在要提取出这个1001,该如何处理阿?
大虾们帮忙啊!
问题点数:50、回复次数:5Top
1 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 17:49:46 得分 5
单是提1001就简单了。
select substring(column1,charindex('1',column1),4)
from yourtableTop
2 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 17:50:15 得分 5
若是有许多行就复杂了。Top
3 楼xluzhong(Ralph)回复于 2005-06-01 18:03:37 得分 20
declare @a table (col1 varchar(255))
insert into @a select '我要送给你1001朵玫瑰'
insert into @a select '我要送给你999朵玫瑰'
declare @col1 varchar(255)
declare test cursor for select col1 from @a
open test
fetch next from test
into @col1
while @@fetch_status=0
begin
declare @return varchar(255)
set @return=''
while patindex('%[0-9]%',@col1)>0
begin
set @return=@return+substring(@col1,patindex('%[0-9]%',@col1),1)
set @col1=stuff(@col1,patindex('%[0-9]%',@col1),1,'')
end
select @return
print @return
fetch next from test
into @col1
end
CLOSE test
DEALLOCATE testTop
4 楼zbyh331(我才刚上路耶!)回复于 2005-06-01 18:15:53 得分 20
declare @a table (col1 varchar(255))
insert into @a select '我要送给你1001朵玫瑰'
insert into @a select '我要送给你999朵玫瑰'
declare @col1 varchar(255)
declare test cursor for select col1 from @a
open test
fetch next from test
into @col1
while @@fetch_status=0
begin
declare @return varchar(255)
set @return=''
while patindex('%[0-9]%',@col1)>0
begin
set @return=@return+substring(@col1,patindex('%[0-9]%',@col1),1)
set @col1=stuff(@col1,patindex('%[0-9]%',@col1),1,'')
end
select @return
print @return
fetch next from test
into @col1
end
CLOSE test
DEALLOCATE test
Top
5 楼liangx326(selina)回复于 2005-06-02 10:46:15 得分 0
谢谢各位,结贴了!Top




