当前位置:网站首页>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
边栏推荐
- QT graphical view frame: element movement
- Principle and general steps of SQL injection
- Intranet penetrating FRP: hidden communication tunnel technology
- Essential basic knowledge of digital image processing
- How can floating point numbers be compared with 0?
- DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
- Opencv learning -- arithmetic operation of image of basic operation
- Research Report on market supply and demand and strategy of surgical stapler industry in China
- . Net delay queue
- The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
猜你喜欢

PR FAQ: how to set PR vertical screen sequence?

~88 running people practice

Opencv learning -- geometric transformation of image processing

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up

Penetration test --- database security: detailed explanation of SQL injection into database principle

Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge

Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database

Working group and domain analysis of Intranet

一图看懂ThreadLocal
随机推荐
TypeError: not enough arguments for format string
[native JS] optimized text rotation effect
Proxifier global agent software, which provides cross platform port forwarding and agent functions
Audio and video technology development weekly | 252
Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
std::shared_ ptr initialization: make_ shared&lt; Foo&gt; () vs shared_ ptr&lt; T&gt; (new Foo) [duplicate]
Model fusion -- stacking principle and Implementation
Research Report on market supply and demand and strategy of China's plastics and polymer industry
Lv166 turned over
Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
Market trend report, technical innovation and market forecast of taillight components in China
[flask] ORM one to many relationship
TypeError: list indices must be integers or slices, not str
ECCV 2022放榜了:1629篇论文中选,录用率不到20%
Actual combat | use composite material 3 in application
Stew in disorder
What is torch NN?
Socks agent tools earthworm, ssoks
[Chongqing Guangdong education] National Open University spring 2019 1396 pharmaceutical administration and regulations (version) reference questions