SQL只要数字

dps898 2010-12-25 03:10:17
create table tb(code varchar(20))
insert into tb values('521501!^%&95')
insert into tb values('5201!^%&95')

select * from tb
drop table tb

得到
52150195
520195
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 元老 2010-12-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
SQL code
create table tb(code varchar(20))
insert into tb values('521501!^%&95')
insert into tb values('5201!^%&95')
go

create function f_numeric(@str varchar(100))
returns varchar(100……
[/Quote]

对于不是数字的字符(如加号 (+)、减号 (-))和有效货币符号(如美元符号 ($))字符,ISNUMERIC 将返回 1,这种情况应该如何判断。
-晴天 2010-12-25
  • 打赏
  • 举报
回复
如果你已知在列中只有某些非数字字符或字符串,则可简单地为:
create table tb(code varchar(20))
insert into tb values('521501!^%&95')
insert into tb values('5201!^%&95')
go
select replace(code,'!^%&','') from tb
drop table tb
/*--------------------------
52150195
520195

(2 行受影响)
*/

永生天地 2010-12-25
  • 打赏
  • 举报
回复
--提取数字
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL
DROP FUNCTION DBO.GET_NUMBER2
GO
CREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
END
RETURN @S
END
GO
--测试
PRINT DBO.GET_NUMBER('呵呵ABC123ABC')
GO
--123
AcHerat 元老 2010-12-25
  • 打赏
  • 举报
回复

--看到别人这么写的!很有用
declare @aa char(80),@bb char(80)
set @aa = 'dfsa1231dad!!!'
set @bb = substring(@aa,PATINDEX( '%[0-9]% ',@aa), len(@aa)-PATINDEX( '%[0-9]% ',@aa)+1)
if patindex( '%[a-z]% ' ,@bb) <> 0
select convert(int,substring(@bb,1,patindex('%[a-z]% ',@bb)-1)) shuzi
else
select @bb

shuzi
-----------
1231

(1 行受影响)
百年树人 2010-12-25
  • 打赏
  • 举报
回复
create table tb(code varchar(20))
insert into tb values('521501!^%&95')
insert into tb values('5201!^%&95')
go

create function f_numeric(@str varchar(100))
returns varchar(100)
as
begin
declare @i int
declare @len int
declare @str1 varchar(100)
set @str1=''
set @len=len(@str)
set @i=1
while @i<=@len
begin
if isnumeric(substring(@str,@i,1))>0
begin
set @str1=@str1+substring(@str,@i,1)
end
else
begin
set @str1=@str1+','
end
set @i=@i+1
end
return replace(@str1,',','')
end
go

select dbo.f_numeric(code) as code from tb
/**
code
-------------------------
52150195
520195

(2 行受影响)
**/

drop function f_numeric
drop table tb

34,590

社区成员

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

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