the following code will return the nth substring after delimiting, either from left or from right; in the particular example the delimiter is comma, and it is looking for the 2nd segment, counting from right
declare
@str_orig VARCHAR(256)='123,345,8,6,7',
@delimit varchar(50) =',',
@loc int =2,
@from_left bit =0
SELECT value, row_number() over( order by( select 1)) as row_num
into #split
FROM STRING_SPLIT(@str_orig, @delimit)
declare @result varchar(256)
if @from_left=1
begin
set @result = ( select top 1 value from #split where row_num=@loc)
end
else
begin
set @result = ( select top 1 value from #split
where row_num= (SELECT MAX(ROw_num) -(@loc-1) FROM #split)
)
end
select @result
5条答案
按热度按时间56lgkhnf1#
The documentation for the function on MSDN is here.
The SQL Fiddle demo is here (with different letters so you can clearly see which letter is extracted).
8dtrkrch2#
Use
substring
functionselect substring('xxx-xxx-xax-xxxxx-xx-x', 10, 1)
pdsfdshx3#
you can use
SUBSTRING
, in your case use...field being the one that returns the value.
wpcxdonn4#
ogsagwnx5#
the following code will return the nth substring after delimiting, either from left or from right; in the particular example the delimiter is comma, and it is looking for the 2nd segment, counting from right