求用SQL识别分离INT型和字符型

xhunanpp 2004-07-05 03:30:47
求用SQL识别分离INT型和字符型

哪一个字符串为: '中国A001'
我要分开为: '中国', 'A', 001
这样怎么分啊,
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-07-14
  • 打赏
  • 举报
回复
declare @t table(Name varchar(10))
insert @t select '中国A001'
union all select '美国B002'
union all select '英国C003'
union all select '小日本D04'
union all select '小日本处死'

select A=left(Name,patindex('%[a-z]%',Name+'a')-1)
,B=substring(Name,patindex('%[a-z]%',Name+'a')
,patindex('%[0-9]%',Name+'0')-patindex('%[a-z]%',Name+'a'))
,C=stuff(Name,1,patindex('%[0-9]%',Name+'0')-1,'')
from @t

/*--结果

A B C
---------- ---------- ----------
中国 A 001
美国 B 002
英国 C 003
小日本 D 04
小日本处死

(所影响的行数为 5 行)
--*/
seekmoon 2004-07-10
  • 打赏
  • 举报
回复
mark
huwgao 2004-07-08
  • 打赏
  • 举报
回复
select left(name,charindex('0',name)-2)
,substring(name,charindex('0',name)-1,1)
,right(name,len(name)-charindex('0',name)+1)
from @t

--字母后必须有0
--只能分离1个字母
xhunanpp 2004-07-08
  • 打赏
  • 举报
回复
没有更好的方法吗
huwgao 2004-07-05
  • 打赏
  • 举报
回复
--支持中间多字符分离

declare @t table(name varchar(50))
insert into @t
select '中国A001' union all
select '美国B002' union all
select '英国C003' union all
select '小日本D04' union all
select '韩国AB04'

select top 26 identity(int,65,1)xh into #t from syscolumns

select left(b.name,minpos-1) as name,
substring(b.name,minpos,maxpos-minpos+1) as string,
right(b.name,len(b.name)-maxpos) as int
from @t as c
left outer join
(select name,max(pos) as maxpos,min(pos) as minpos
from
(select name,charindex(char(xh),name) as pos
from @t
cross join #t) as a
where a.pos<>0
group by a.name) as b
on c.name=b.name

drop table #t
internetcsdn 2004-07-05
  • 打赏
  • 举报
回复
高手.
huwgao 2004-07-05
  • 打赏
  • 举报
回复
declare @t table(name varchar(50))
insert into @t
select '中国A001' union all
select '美国B002' union all
select '英国C003' union all
select '小日本D04'

select top 26 identity(int,65,1)xh into #t from syscolumns

select left(b.name,pos-1) as name,
substring(b.name,pos,1) as string,
right(b.name,len(b.name)-pos) as int
from (select name,charindex(char(xh),name) as pos from @t cross join #t) as a
inner join @t as b
on a.pos>0 and a.name=b.name

drop table #t
internetcsdn 2004-07-05
  • 打赏
  • 举报
回复
只能分离数字.

字母就不知怎办了.
internetcsdn 2004-07-05
  • 打赏
  • 举报
回复
--参考:

set @in ='abc123de4'
declare @n int
declare @i int
set @n=len(@in)
set @i=1
declare @s varchar(50)
set @s=''
while @i<=@n
begin
if isnumeric(substring(@in,@i,1))=1
begin
set @s=@s+substring(@in,@i,1)
end
set @i=@i+1
end
print @s

--------
1234
xhunanpp 2004-07-05
  • 打赏
  • 举报
回复
如果我有一列字段

Name
-------------------------
中国A001
美国B002
英国C003
小日本D04

分成

Name String Int
-----------------------
中国 A 001
美国 B 002
英国 C 003
小日本 D 04

34,590

社区成员

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

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