MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3201字,纯文字阅读大概需要5分钟。
内容图文
![MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX](/upload/InfoBanner/zyjiaocheng/523/f57229d8e20e499e94343eb2a7dd9da0.jpg)
在博客里看《mysql根据分隔符将一行数据拆分成多行数据》文章,
MYSQL中有系统函数 SUBSTRING_INDEX(), 方便地实现了“一行数据拆分成多行”,
SQL SERVER使用都可没这么好了。因此,我在SQL SERVER 2008中仿写了一个自定义函数 f_substring_index() 。
详细代码如下:
/*
Returns the substring from string str before count occurrences of the
delimiter delim. If count is positive, everything to the left of the
final delimiter (counting from the left) is returned. If count is
negative, everything to the right of the final delimiter (counting from
the right) is returned. SUBSTRING_INDEX() performs a case-sensitive
match when searching for delim.
*/
alter FUNCTION DBO.F_SUBSTRING_INDEX(
@str NVARCHAR(4000),
@delim NVARCHAR(128),
@count INT
)RETURNS NVARCHAR(256)
AS
BEGIN
-- F_SUBSTRING_INDEX():截斷函數,同MYSQL的SUBSTRING_INDEX();
-- @str: 要處理的字符串
-- @delim: 分隔符
-- @count: 計數,如果@COUNT是正數,那麼就是從左往右數,第N個分隔符的左邊的全部內容
-- 相反,如果是負數,那麼就是從右邊開始數,第N個分隔符右邊的所有內容。
--
DECLARE @RESULT NVARCHAR(256);
declare @index int;
declare @i int =0 ;
DECLARE @pos int=0 -- 記錄位置
DECLARE @times INT =0 -- 記錄找的次數
declare @stringLength int = 0;
declare @ReverseStr varchar(4000);
set @stringLength = LEN(@str);
if @count>0
begin
-- 1、先考慮存在分隔符
-- while @i < @stringLength
begin
WHILE(@times<@count)
BEGIN
begin
SET @times = @times+1
set @pos = CHARINDEX(@delim,@str,@pos+1)
end;
END
end
-- 2、考慮不存在分隔符,計數超出分隔符總數量
if @pos = 0
set @index = @stringLength
else
set @index = @pos
-- cal:
if @pos>=1
set @RESULT = SUBSTRING(@str,1,@index -1);
else
set @RESULT= SUBSTRING(@str,1,@stringLength);
end;
else IF @count < 0
begin
SET @pos =0;
set @ReverseStr = REVERSE(@str);
-- 1、先考慮存在分隔符
-- while @i < @stringLength
begin
WHILE(@times<abs(@count))
BEGIN
begin
SET @times = @times+1
set @pos = CHARINDEX(REVERSE(@delim),@ReverseStr,@pos+1)
end;
END
end
-- 2、考慮不存在分隔符,計數超出分隔符總數量
if @pos = 0
set @index = @stringLength
else
set @index = @pos
-- cal:
if @pos>=1
set @RESULT = reverse(SUBSTRING(@ReverseStr,1,@index -1));
else
set @RESULT= reverse(SUBSTRING(@ReverseStr,1,@stringLength));
end;
else
set @RESULT = N‘‘;
RETURN @RESULT;
END;
go
调试实例:
SELECT DBO.F_SUBSTRING_INDEX( ‘Aa,Bb,Cc,df,hgk‘,‘,‘,2) AS SUBSTRING_INDEX2
,DBO.F_SUBSTRING_INDEX( ‘Aa,Bb,Cc,df,hgk‘,‘,‘,0) AS REVERSE_INDEX0
,DBO.F_SUBSTRING_INDEX( ‘Aa,Bb,Cc,df,hgk‘,‘,‘,-1) AS REVERSE_INDEX
,DBO.F_SUBSTRING_INDEX( ‘Aa,Bb,Cc,df,hgk‘,‘,‘,1) AS REVERSE_INDEX1
-- 返回:
/*
SUBSTRING_INDEX2 REVERSE_INDEX0 REVERSE_INDEX REVERSE_INDEX1
---------------- -------------- ------------- --------------
Aa,Bb NULL hgk Aa
*/
go
MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX
标签:declare server searching 超出 orm lte sel turn from
本文系统来源:https://www.cnblogs.com/samrv/p/12793976.html
内容总结
以上是互联网集市为您收集整理的MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX全部内容,希望文章能够帮你解决MSSQL:仿写MYSQL的substring_index 截断函数 F_SUBSTRING_INDEX所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。