下面只是部分内容:
ID NAME
1 张;王;𞈡
2 赵;钱;孙;񀕶
3 李𾣪󰶤
4 韩
.
.
.
转换后
ID NAME1 NAME2 NAME3
1 张 王
2 赵 钱 孙
3 李
4 韩
求高人指点!~如果单纯的按照“;”分割字符,最后还要处理“李𾣪”这样的字段,能不能一次性处理完啊?
菜鸟求解!~~~~~~~~~~~~~~~~~~~~~~~
ID NAME
1 张;王;𞈡
2 赵;钱;孙;񀕶
3 李𾣪󰶤
4 韩
.
.
.
转换后
ID NAME1 NAME2 NAME3
1 张 王
2 赵 钱 孙
3 李
4 韩
求高人指点!~如果单纯的按照“;”分割字符,最后还要处理“李𾣪”这样的字段,能不能一次性处理完啊?
菜鸟求解!~~~~~~~~~~~~~~~~~~~~~~~
解决方案 »
- 如何写这个sql语句
- =========================问个sql问题========================
- 请教个sql语句的问题....
- 想要学习SQLSERVER存储过程,有什么好书,大家介绍一下哈
- 高手进:SqlServer CheckSum函数的原理是什么?
- 怎样做出库呀,用update更新库存吗?
- 解决就给100分(排序问题,有点难度)
- 对sql数据库中的表做updete时如何判断那些数据项背更新为不同的值了?
- 将如200606的varchar转换为datetime的问题
- 在企业管理器中误删除表,没有备份文件,如何恢复(undo)?在线等待。。。。
- ms server 2005 创建数据库
- sql组合数的问题
而且必須保證你的例子中已經存在所有可能出現的情況,,,
然後就一個一個搜索了,,
建議別在SQL裏做,,,重續裏去一個一個判斷容易寫得多
這個要在SQL裏面作只有做
xml動態拼接才有辦法,,,好難搞,,,,哪位有愛的大大來寫
SQL裏面做就是,,,xml語句拼接了,,痛苦,,
-- Author :SQL77(只为思齐老)
-- Date :2010-01-12 10:30:31
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
go
create table [TB]([ID] int,[NAME] varchar(19))
insert [TB]
select 1,'张;王;𞈡' union all
select 2,'赵;钱;孙;񀕶' union all
select 3,'李𾣪󰶤' union all
select 4,'韩'
--------------开始查询--------------------------select ID,
LEFT(SUBSTRING(T.NAME,NUMBER,CHARINDEX(';',T.NAME+';',NUMBER)-NUMBER),1)AS NAME
from [TB] T,MASTER..SPT_VALUES WHERE TYPE='P'
AND SUBSTRING(';'+T.NAME,NUMBER,1)=';'
AND PATINDEX('%[吖-做]%',
SUBSTRING(T.NAME,NUMBER,CHARINDEX(';',T.NAME+';',NUMBER)-NUMBER))>0
----------------结果----------------------------
/* (所影响的行数为 4 行)ID NAME
----------- ----
1 张
1 王
2 赵
2 钱
2 孙
3 李
4 韩(所影响的行数为 7 行)
*/那个又要行转列,感觉麻烦
--要去除的是不是就是 & ,# ,0,1,2,3,4,5,6,7,8,9 这十二个字符?
--如果是的话,就写一个function将这些字符都去掉再处理
--感觉资料好混乱啊
create function dbo.fn_replace(@name nvarchar(100))
returns nvarchar(100)
AS
begin
declare @re nvarchar(100)
declare @table table(col nchar(01),name nvarchar(100))
insert into @table
select '#',@name
union select '&',@name
union select '0',@name
union select '1',@name
union select '2',@name
union select '3',@name
union select '4',@name
union select '5',@name
union select '6',@name
union select '7',@name
union select '8',@name
union select '9',@name
Update @table
set @name=isnull(@re,name),
@re=replace(@name,col,''),
name=@re
select @re=name from @table where col='9' return @reEnd
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[NAME] varchar(19))
insert [tb]
select 1,'张;王;𞈡' union all
select 2,'赵;钱;孙;񀕶' union all
select 3,'李𾣪󰶤' union all
select 4,'韩'
--函数
create function dbo.getitemname(@name varchar(50),@pos int)
returns varchar(30)
as
begin
declare @ret varchar(30)
declare @table table(id int identity(1,1),name varchar(30))
insert into @table
select substring(@name,number,charindex(';',@name+';',number)-number)
from master..spt_values
where type='p'
and substring(';'+@name,number,1)=';'
select @ret=name from @table where id = @pos
if isnull(@ret,'')!=''
begin
select @ret=case patindex('%[吖-做]%',@ret) when 0 then null
else case patindex('%[^吖-做]%',@ret) when 0 then @ret else
substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end
end
end
return @ret
end--查询
select ID,
dbo.getitemname([NAME],1) as [NAME1],
dbo.getitemname([NAME],2) as [NAME2],
dbo.getitemname([NAME],3) as [NAME3]
from [tb]
--结果
------------------------------
1 张 王 NULL
2 赵 钱 孙
3 李 NULL NULL
4 韩 NULL NULLcode]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([ID] int,[NAME] varchar(19))
insert [tb]
select 1,'张;王;𞈡' union all
select 2,'赵;钱;孙;񀕶' union all
select 3,'李𾣪󰶤' union all
select 4,'韩'
--函数
create function dbo.getitemname(@name varchar(50),@pos int)
returns varchar(30)
as
begin
declare @ret varchar(30)
declare @table table(id int identity(1,1),name varchar(30))
insert into @table
select substring(@name,number,charindex(';',@name+';',number)-number)
from master..spt_values
where type='p'
and substring(';'+@name,number,1)=';'
select @ret=name from @table where id = @pos
if isnull(@ret,'')!=''
begin
select @ret=case patindex('%[吖-做]%',@ret) when 0 then null
else case patindex('%[^吖-做]%',@ret) when 0 then @ret else
substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end
end
end
return @ret
end --查询
select ID,
dbo.getitemname([NAME],1) as [NAME1],
dbo.getitemname([NAME],2) as [NAME2],
dbo.getitemname([NAME],3) as [NAME3]
from [tb]
--结果
------------------------------
1 张 王 NULL
2 赵 钱 孙
3 李 NULL NULL
4 韩 NULL NULL
--有个小问题
--patindex('%[吖-做]%',@ret) 判断中文字符的起始位置
create function dbo.getitemname(@name varchar(50),@pos int)
returns varchar(30)
as
begin
declare @ret varchar(30)
declare @table table(id int identity(1,1),name varchar(30)) insert into @table
select substring(@name,number,charindex(';',@name+';',number)-number)
from master..spt_values
where type='p'
and substring(';'+@name,number,1)=';' select @ret=name from @table where id = @pos if isnull(@ret,'')!=''
begin
select @ret=stuff(@ret,1,patindex('%[吖-做]%',@ret)-1,'')--加上这句
select @ret=case patindex('%[吖-做]%',@ret) when 0 then null
else case patindex('%[^吖-做]%',@ret) when 0 then @ret else
substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end
end
end
return @ret
end