if object_id('[TB]') is not null drop table [TB] create table [TB]([col] varchar(200)) insert [TB] select '张三 11111111 中国 男' union all select '李四 2222 上海 女' union all select '王五 111111 北京 男'go create function dbo.f_s(@s varchar(8000)) returns varchar(8000) as begin set @s=replace(@s,' ','.') while charindex('..',@s)>0 set @s=replace(@s,'..','.') return @s end goselect parsename(dbo.f_s(col),4), parsename(dbo.f_s(col),3), parsename(dbo.f_s(col),2), parsename(dbo.f_s(col),1) from TB
create table tb(id int,s nvarchar(200)) insert into tb select 1,'张三 11111111 中国 男' insert into tb select 2,'李四 2222 上海 女' insert into tb select 3,'王五 111111 北京 男' go select id, max(case when rn=1 then s else '' end)姓名, max(case when rn=2 then s else '' end)金额, max(case when rn=3 then s else '' end)地址, max(case when rn=4 then s else '' end)性别 from( select row_number()over(partition by id order by number)rn,id,s from( select a.id,number,substring(a.s,b.number,charindex(' ',a.s+' ',b.number+1)-b.number)s from tb a,master..spt_values b where b.type='p' and b.number<=len(a.s) and substring(a.s,b.number,1)<>' ' and substring(' '+a.s,b.number,1)=' ' )t)b group by id /* id 姓名 金额 地址 性别 ----------- -------------- --------------------------------------------------------------------------------------------------- 1 张三 11111111 中国 男 2 李四 2222 上海 女 3 王五 111111 北京 男(3 行受影响) */ go drop table tb
PARSENAME
返回对象名的指定部分。可以检索的对象部分有对象名、所有者名称、数据库名称和服务器名称。说明 PARSENAME 函数不表明所指定名称的对象是否存在,而只是返回给定对象名的指定部分。
语法
PARSENAME ( 'object_name' , object_piece ) 参数
'object_name'要检索其指定部分的对象名。object_name 是 sysname 值。本参数是可选的合法对象名。如果该对象名的所有部分均符合要求,则该名称由以下四部分组成:服务器名称、数据库名称、所有者名称和对象名。object_piece要返回的对象部分。object_piece 是 int 值,可以为下列值。Value 描述
1 对象名
2 所有者名称
3 数据库名称
4 服务器名称
返回类型
nchar注释
如果符合下列条件之一,则 PARSENAME 返回 NULL 值: object_name 或 object_piece 为 NULL 值。
发生语法错误。
所请求的对象部分长度为 0,并且是无效的 Microsoft® SQL Server™ 标识符。零长度的对象名将导致整个合法名称无效。
示例
本示例使用 PARSENAME 返回有关 pubs 数据库中 authors 表的信息。USE pubs
SELECT PARSENAME('pubs..authors', 1) AS 'Object Name'
SELECT PARSENAME('pubs..authors', 2) AS 'Owner Name'
SELECT PARSENAME('pubs..authors', 3) AS 'Database Name'
SELECT PARSENAME('pubs..authors', 4) AS 'Server Name'下面是结果集:Object Name
------------------------------
authors (1 row(s) affected)Owner Name
------------------------------
(null) (1 row(s) affected)Database Name
------------------------------
pubs (1 row(s) affected)Server Name
------------------------------
(null) (1 row(s) affected)
if object_id('[TB]') is not null drop table [TB]
create table [TB]([col] varchar(200))
insert [TB]
select '张三 11111111 中国 男'
union all
select '李四 2222 上海 女'
union all
select '王五 111111 北京 男'go
create function dbo.f_s(@s varchar(8000)) returns varchar(8000)
as
begin
set @s=replace(@s,' ','.')
while charindex('..',@s)>0
set @s=replace(@s,'..','.')
return @s
end
goselect parsename(dbo.f_s(col),4),
parsename(dbo.f_s(col),3),
parsename(dbo.f_s(col),2),
parsename(dbo.f_s(col),1)
from TB
insert into tb select 1,'张三 11111111 中国 男'
insert into tb select 2,'李四 2222 上海 女'
insert into tb select 3,'王五 111111 北京 男'
go
select id,
max(case when rn=1 then s else '' end)姓名,
max(case when rn=2 then s else '' end)金额,
max(case when rn=3 then s else '' end)地址,
max(case when rn=4 then s else '' end)性别
from(
select row_number()over(partition by id order by number)rn,id,s from(
select a.id,number,substring(a.s,b.number,charindex(' ',a.s+' ',b.number+1)-b.number)s
from tb a,master..spt_values b
where b.type='p' and b.number<=len(a.s) and substring(a.s,b.number,1)<>' ' and substring(' '+a.s,b.number,1)=' '
)t)b group by id
/*
id 姓名 金额 地址 性别
----------- -------------- ---------------------------------------------------------------------------------------------------
1 张三 11111111 中国 男
2 李四 2222 上海 女
3 王五 111111 北京 男(3 行受影响)
*/
go
drop table tb