字符串截取。。。龙哥在吗?帮个忙。。。

jingqianbao123 2009-09-02 05:47:26
餐饮集团/华东区管理处/中央厨房/营销部
餐饮集团
餐饮集团/华东区管理处
餐饮集团/华东区管理处/营销部
餐饮集团/华东区管理处/中央厨房/营销1部
餐饮集团/华东区管理处/中央厨房/营销2部
餐饮集团/华东区管理处/中央厨房/营销2部


我现在想截取第二个 “/” 字符和第三个“/”字符之间的数据怎么做??貌似mssql没split函数。。。


注:有些数据是没有"/"字符的。。就像第二条一样。。。
...全文
175 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangqinggao 2009-09-03
  • 打赏
  • 举报
回复
学习了。
jingqianbao123 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 htl258 的回复:]
如果一定要有两个"/"才取中间数,函数改为如下:
SQL code--> 生成测试数据表:tbIfnotobject_id('[tb]')isnullDroptable[tb]GoCreatetable[tb]([col]nvarchar(21))Insert[tb]Select N'餐饮集团/华东区管理处/中央厨房/营销部'unionallSelect N'餐饮集团'unionallSelect N'餐饮集团/华东区管理处'unionallSelect N'餐饮集团/华东区管理处/营销部'unionallSelect N'餐饮集团/华东区管理处/中央厨房/营销1部'unionallSelect N'餐饮集团/华东区管理处/中央厨房/营销2部'unionallSelect N'餐饮集团/华东区管理处/中央厨房/营销2部'Go--Select * from [tb]-->SQL查询如下:Ifnotobject_id('[fn_str]')isnullDropfunction[fn_str]GoCreatefunction fn_str(@colvarchar(8000))returnsvarchar(1000)asbeginiflen(@col)-len(replace(@col,'/',''))<3set@col=''elsebeginset@col=stuff(@col,1,charindex('/',@col),'')+'/'set@col=left(@col,charindex('/',@col)-1)endreturn(@col)endgoselect dbo.fn_str(COL) colfrom tb/*
col
-----------------------
华东区管理处



华东区管理处
华东区管理处
华东区管理处

(7 行受影响)*/
[/Quote]

不知道可否传入两个值,比如传入m 和n 就是截取这字符串中第m次和第n次出现这这个'/'字符之间的字符。
soft_wsx 2009-09-02
  • 打赏
  • 举报
回复
看来是我误解意思了!顶贴算了!
jinjazz 2009-09-02
  • 打赏
  • 举报
回复
参考
http://blog.csdn.net/jinjazz/archive/2009/08/18/4457417.aspx
xupeihuagudulei 2009-09-02
  • 打赏
  • 举报
回复
都是牛人。深夜帮顶
licry01 2009-09-02
  • 打赏
  • 举报
回复

If not object_id('[test_tb01]') is null
begin
Drop table [test_tb01];
end
Go
-----------------------------------------------------------
-----------------------------------------------------------
Create table [test_tb01]
(
id int IDENTITY(1,1) NOT NULL,
[field] nvarchar(200) COLLATE Chinese_PRC_CI_AS NOT NULL
)
Insert [test_tb01]([field])
Select N'餐饮集团/华东区管理处/中央厨房/营销部' union all
Select N'餐饮集团' union all
Select N'餐饮集团/华东区管理处' union all
Select N'餐饮集团/华东区管理处/营销部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销1部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部'
Go
-----------------------------------------------------------
-----------------------------------------------------------
CREATE function [dbo].[FUNCTION_AAAA](
@value nvarchar(200), --要提取的源串, 如'餐饮集团/华东区管理处/中央厨房/营销1部
@flag nvarchar(10) , --要查找的分隔符号, 如 '/'
@flag_begin_pos int , --源串中分隔符号出现的次数, 以该次数作为提取字串的起始
@flag_end_pos int --源串中分隔符号出现的次数, 以该次数作为提取字串的结束
)
returns nvarchar(200)
as
begin
declare @rtv nvarchar(200);
declare @pos1 int, @pos2 int, @num int, @pos int;
set @pos = 0 ;
set @pos1 = 0 ; --保存要返回的字串位于源串的起始位置
set @pos2 = 0 ; --保存要返回的字串位于源串的结束位置
set @num = 0 ; --保存遍历过程中分隔符合一共出现了多少次

if @value is null
begin
return null;
end
if @flag is null
begin
return null;
end
if @flag_begin_pos is null
begin
return null;
end
if @flag_end_pos is null
begin
return null;
end
if @flag_begin_pos >= @flag_end_pos
begin
return null;
end

while 1>0
begin
Set @pos = charindex(@flag,@value,@pos);

if @pos = 0
begin
break;
end
else
begin
set @num = @num + 1;
if @num=@flag_begin_pos
begin
set @pos1=@pos + 1;
end
else
begin
if @num = @flag_end_pos
begin
set @pos2 = @pos ;
break;
end
end
set @pos = @pos + 1;
end
end

if @num <> @flag_end_pos
begin
set @rtv = N'NULL' ; --没有在源串中找到合适的字串, 返回null
end
else
begin
set @rtv = SUBSTRING(@value,@pos1,@pos2-@pos1); ;
end

return @rtv;
end
GO
-----------------------------------------------------------
-----------------------------------------------------------
Select *, dbo.[FUNCTION_AAAA](field,N'/',2,3) from [test_tb01];
/*
1 餐饮集团/华东区管理处/中央厨房/营销部 中央厨房
2 餐饮集团 NULL
3 餐饮集团/华东区管理处 NULL
4 餐饮集团/华东区管理处/营销部 NULL
5 餐饮集团/华东区管理处/中央厨房/营销1部 中央厨房
6 餐饮集团/华东区管理处/中央厨房/营销2部 中央厨房
7 餐饮集团/华东区管理处/中央厨房/营销2部 中央厨房
*/


Select *, dbo.[FUNCTION_AAAA](field,N'/',1,3) from [test_tb01];
/*
1 餐饮集团/华东区管理处/中央厨房/营销部 华东区管理处/中央厨房
2 餐饮集团 NULL
3 餐饮集团/华东区管理处 NULL
4 餐饮集团/华东区管理处/营销部 NULL
5 餐饮集团/华东区管理处/中央厨房/营销1部 华东区管理处/中央厨房
6 餐饮集团/华东区管理处/中央厨房/营销2部 华东区管理处/中央厨房
7 餐饮集团/华东区管理处/中央厨房/营销2部 华东区管理处/中央厨房
*/


Select *, dbo.[FUNCTION_AAAA](field,N'/',1,2) from [test_tb01];
/*
1 餐饮集团/华东区管理处/中央厨房/营销部 华东区管理处
2 餐饮集团 NULL
3 餐饮集团/华东区管理处 NULL
4 餐饮集团/华东区管理处/营销部 华东区管理处
5 餐饮集团/华东区管理处/中央厨房/营销1部 华东区管理处
6 餐饮集团/华东区管理处/中央厨房/营销2部 华东区管理处
7 餐饮集团/华东区管理处/中央厨房/营销2部 华东区管理处
*/
htl258_Tony 2009-09-02
  • 打赏
  • 举报
回复
如果一定要有两个"/"才取中间数,函数改为如下:
--> 生成测试数据表:tb

If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([col] nvarchar(21))
Insert [tb]
Select N'餐饮集团/华东区管理处/中央厨房/营销部' union all
Select N'餐饮集团' union all
Select N'餐饮集团/华东区管理处' union all
Select N'餐饮集团/华东区管理处/营销部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销1部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部'
Go
--Select * from [tb]

-->SQL查询如下:
If not object_id('[fn_str]') is null
Drop function [fn_str]
Go
Create function fn_str(@col varchar(8000))
returns varchar(1000)
as
begin
if len(@col)-len(replace(@col,'/',''))<3
set @col=''
else
begin
set @col=stuff(@col,1,charindex('/',@col),'')+'/'
set @col=left(@col,charindex('/',@col)-1)
end
return(@col)
end
go

select dbo.fn_str(COL) col from tb
/*
col
-----------------------
华东区管理处



华东区管理处
华东区管理处
华东区管理处

(7 行受影响)
*/
htl258_Tony 2009-09-02
  • 打赏
  • 举报
回复
--> 生成测试数据表:tb

If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([col] nvarchar(21))
Insert [tb]
Select N'餐饮集团/华东区管理处/中央厨房/营销部' union all
Select N'餐饮集团' union all
Select N'餐饮集团/华东区管理处' union all
Select N'餐饮集团/华东区管理处/营销部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销1部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部' union all
Select N'餐饮集团/华东区管理处/中央厨房/营销2部'
Go
--Select * from [tb]

-->SQL查询如下:
If not object_id('[fn_str]') is null
Drop function [fn_str]
Go
Create function fn_str(@col varchar(8000))
returns varchar(1000)
as
begin
if charindex('/',@col)=0
set @col=''
else
begin
set @col=stuff(@col,1,charindex('/',@col),'')+'/'
set @col=left(@col,charindex('/',@col)-1)
end
return(@col)
end
go

select dbo.fn_str(COL) col from tb
/*
col
--------------------
华东区管理处

华东区管理处
华东区管理处
华东区管理处
华东区管理处
华东区管理处

(7 行受影响)
*/
soft_wsx 2009-09-02
  • 打赏
  • 举报
回复
declare @s nvarchar(4000)
set @s=N'餐饮集团/华东区管理处/中央厨房/营销2部'
while CHARINDEX('/',@s+'/')>1
begin
print left(@s,charindex('/',@s)-1)
set @s=STUFF(@s,1,charindex('/',@s+'/'),N'')
end
lihan6415151528 2009-09-02
  • 打赏
  • 举报
回复
没有的就直接返回啊。
lihan6415151528 2009-09-02
  • 打赏
  • 举报
回复

create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
--实现split功能 的函数
--date :2003-10-14
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>''
insert @temp values(@SourceSql)
return
end


select * from dbo.f_split('餐饮集团/华东区管理处/中央厨房/营销1部 ','/')


a
--------------------------
餐饮集团
华东区管理处
中央厨房
营销1部

(所影响的行数为 4 行)
--小F-- 2009-09-02
  • 打赏
  • 举报
回复
没有'/'的怎么处理啊?
lihan6415151528 2009-09-02
  • 打赏
  • 举报
回复
lihan6415151528 2009-09-02
  • 打赏
  • 举报
回复

create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
--实现split功能 的函数
--date :2003-10-14
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>''
insert @temp values(@SourceSql)
return
end

select * from dbo.f_split('1,2,3,4',',')

a
--------------------
1
2
3
4

(所影响的行数为 4 行)



22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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