sql2005只取字段中的 数字 有函数可以用吗????????

wym840713 2009-05-25 05:51:06
部门-1011
部门-1022


要求结果
1011
1022
...全文
178 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wym840713 2009-05-26
  • 打赏
  • 举报
回复
谢谢12楼
htl258_Tony 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wym840713 的回复:]
3楼的处理:
部门-1011-财务科

结果是: 1011-财务科
[/Quote]
11楼的试了吗?
wym840713 2009-05-25
  • 打赏
  • 举报
回复
3楼的处理:
部门-1011-财务科

结果是: 1011-财务科
htl258_Tony 2009-05-25
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb](col varchar(20))
insert [tb] select '部门-1011-财务科'
insert [tb] select '部门-1022'

select left(substring(col,patindex('%[0-9]%',col),len(col+'-')-patindex('%[0-9]%',col)),charindex('-',substring(col,patindex('%[0-9]%',col),len(col+'-')-patindex('%[0-9]%',col))+'-')-1) as col from tb
/*
col
--------------------
1011
1022

(2 行受影响)
*/
you_tube 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wym840713 的回复:]
刚才没有清楚 ,可能数字后面也会有汉字的

部门-1011-财务科
[/Quote]
这个就行
[Quote=引用 3 楼 jinjazz 的回复:]
SQL codeselect姓名=left(str,patindex('%[0-9]%',str+'0')-1)
,证件号=stuff(str,1,patindex('%[0-9]%',str+'0')-1,'')from(--这后面是直接写测试数据,你可以直接写表名selectstr=N'张三612345678901234'unionallselectstr=N'张三三612345678901234'unionallselectstr=N'李四612345678901234567'unionallselectstr=N'李四四612345678901234567'unionallselectstr=N'王五(护照号)12345678'unionallselectstr=N'王五五(护照号…
[/Quote]
Jamin_Liu 2009-05-25
  • 打赏
  • 举报
回复
declare @source table
(
context varchar(20)
)

insert into @source
values('部门-001'),
('部门-002');

select right(context,PATINDEX('%[0-9]%',context)-1)
from @source
wym840713 2009-05-25
  • 打赏
  • 举报
回复
刚才没有清楚 ,可能数字后面也会有汉字的

部门-1011-财务科
csdyyr 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csdyyr 的回复:]
SQL codeselectright(col,5)fromtb
[/Quote]

DECLARE @TB TABLE([col] NVARCHAR(7))
INSERT @TB
SELECT N'部门-1011' UNION ALL
SELECT N'部门-1022'

SELECT RIGHT(col,4) as col
FROM @TB
/*
col
----
1011
1022
*/

SQL77 2009-05-25
  • 打赏
  • 举报
回复
SELECT RIGHT(RTRIM(@STR),4) FROM TB
htl258_Tony 2009-05-25
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb](col varchar(20))
insert [tb] select '部门-1011'
insert [tb] select '部门-1022'

select right(col,len(col)-charindex('-',col)) as col from tb
/*
col
--------------------
1011
1022

(2 行受影响)
*/
Zoezs 2009-05-25
  • 打赏
  • 举报
回复

declare @STR varchar(50)
set @STR=N'部门-1011'
select right(@STR,charindex('-',@STR)+1)
jinjazz 2009-05-25
  • 打赏
  • 举报
回复
select 姓名=left(str,patindex('%[0-9]%',str+'0')-1)
,证件号=stuff(str,1,patindex('%[0-9]%',str+'0')-1,'')
from( -- 这后面是直接写测试数据,你可以直接写表名
select str=N'张三612345678901234' union all
select str=N'张三三612345678901234' union all
select str=N'李四612345678901234567' union all
select str=N'李四四612345678901234567' union all
select str=N'王五(护照号)12345678' union all
select str=N'王五五(护照号)12345678'
)a
/*
张三 612345678901234
张三三 612345678901234
李四 612345678901234567
李四四 612345678901234567
王五(护照号) 12345678
王五五(护照号) 12345678
*/
htl258_Tony 2009-05-25
  • 打赏
  • 举报
回复
select right(col,len(col)-charindex('-',col)) as col from tb
csdyyr 2009-05-25
  • 打赏
  • 举报
回复
select right(col,5) from tb

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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