当前位置:网站首页>Sql实现Split
Sql实现Split
2022-07-04 14:54:00 【全栈程序员站长】
create function [dbo].[Get_StrArrayLength] ( @str varchar(max), –要分割的字符串 @split varchar(10) –分隔符号 ) returns int as begin declare @location int declare @start int declare @length int –去掉前后空格 set @str=ltrim(rtrim(@str)) –分隔符第一次出现的位置 set @location=charindex(@split,@str) –设置分隔后的字符串长度为1 set @length=1
while @location<>0 begin set @[email protected]+1 set @location=charindex(@split,@str,@start) set @[email protected]+1 end return @length end
drop function [dbo].[Get_StrArrayStrOfIndex] go create function [dbo].[Get_StrArrayStrOfIndex] ( @str varchar(max), –要分割的字符串 @split varchar(10), –分隔符号 @index int –取第几个元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int declare @pos int if @index<=0 return ” set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @pos=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location<>0 and @index>@next and @pos<@index begin set @[email protected][email protected] set @location=charindex(@split,@str,@start) set @[email protected]+1 set @[email protected]+1 end if @location =0 if @pos<>@index return ” else select @location =len(@str)+1 –这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。 return substring(@str,@start,@[email protected]) end
declare @id varchar(30) select @id=dbo.Get_StrArrayStrOfIndex(‘4321,2223,333,5554′,’,’,2)
我的改进版本,可像Hashtable一样访问
drop function [dbo].[Get_ArrayStrOfName] Go
create function [dbo].[Get_ArrayStrOfName] ( @str varchar(5000), –要分割的字符串 @split varchar(1), –分隔符号 @itemSplit varchar(1), –元素分割符 @item varchar(20) –取哪个元素 ) returns varchar(1024) As Begin declare @location int –找到子串的位置 declare @tempItem varchar(50) –当前项 declare @tempItemName varchar(20) –当前项的名称 set @str=ltrim(rtrim(@str)) set @tempItem=” set @tempItemName=” while len(@str)>0 Begin —–找到一个子项– set @location=charindex(@split,@str,1) if @location>0 Begin set @tempItem=substring(@str,1,@location-1) set @str=substring(@str,@location+1,len(@str)) End Else Begin set @[email protected] set @str=” end ———-判断当前子项的名称————– set @location=charindex(@itemSplit,@tempItem,1) if @location>0 Begin set @tempItemName=substring(@tempItem,1,@location-1) if @[email protected] return substring(@tempItem,@location+1,len(@tempItem)) End End return ” End Go
declare @id varchar(30) select @id=dbo.Get_ArrayStrOfName(‘[email protected],[email protected],[email protected],[email protected]’, ‘,’, ‘@’, ‘ff’) print @id
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/110915.html原文链接:https://javaforall.cn
边栏推荐
- A trap used by combinelatest and a debouncetime based solution
- Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
- c# 实现定义一套中间SQL可以跨库执行的SQL语句
- Anta is actually a technology company? These operations fool netizens
- Accounting regulations and professional ethics [8]
- Move, say goodbye to the past again
- 2022年九大CIO趨勢和優先事項
- Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia
- Hidden communication tunnel technology: intranet penetration tool NPS
- TypeError: list indices must be integers or slices, not str
猜你喜欢
[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection
DIY a low-cost multi-functional dot matrix clock!
Vscode setting outline shortcut keys to improve efficiency
Penetration test --- database security: detailed explanation of SQL injection into database principle
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
~89 deformation translation
Understand asp Net core - Authentication Based on jwtbearer
I let the database lock the table! Almost fired!
Move, say goodbye to the past again
The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
随机推荐
Selenium browser (2)
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Function test - knowledge points and common interview questions
Move, say goodbye to the past again
The four most common errors when using pytorch
TypeError: not enough arguments for format string
Variable cannot have type 'void'
.Net 应用考虑x64生成
Hidden communication tunnel technology: intranet penetration tool NPS
How to save the contents of div as an image- How to save the contents of a div as a image?
Preliminary practice of niuke.com (10)
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
Unity script API - time class
一图看懂ThreadLocal
A trap used by combinelatest and a debouncetime based solution
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Market trend report, technical innovation and market forecast of electrochromic glass and devices in China and Indonesia