Как разрезать строку на части в SQL по некоторому символу
Функция позволяет извлечь часть строки из другой строки по разделителю.
Пример использования:
select dbo.str_splitPart('111,222,333', ',', 1) -- 111
select dbo.str_splitPart('111,222,333', ',', 2) -- 222,333
@part - либо 1, либо 2
ALTER function [dbo].[str_splitPart] (
@value nvarchar(max),
@divider nvarchar(12) = ',',
@part int = 2
)
returns nvarchar(max) as
begin
declare @res nvarchar(max) = ''
if(CHARINDEX(@divider,@value)>0) begin
if(@part=1) begin
set @res = LEFT(@value,CHARINDEX(@divider,@value)-1)
end else
begin
set @res = RIGHT(@value,LEN(@value) - CHARINDEX(@divider,@value) -(LEN(@divider)-1))
end
end else begin
if(@part=1) begin
set @res = @value
end else
begin
set @res = ''
end
end
return @res
end
Additional
SQL. How to move to a new line in a string variable in SQL
SQL. Output HTML markup in crude form (as tags) in SQL
SQL. How to enter a date in a string in the desired format
SQL. How to use select from Storage
SQL. How to write a number in SQL in words
SQL. How to massively upload data to a Database table via CSV (Excel)
SQL-tool for creating personal accounts on the site
The essence of the approach and the history of the creation of Falcon Space

Falcon Space Platform
This is a reduction in the cost of ownership
at the expense of fewer people to support
This is a quick change
while using the program
This is a modern interface
full adaptation for mobile devices
MS SQL web applications. Affiliate program for developers and web studios
You can develop on your own or collaborate with us on Falcon Space web development using only SQL and HTML.
See examples with SQL code
Platform documentation

Component demo stand
At the stand you can see various components in action - tables, forms, modal windows, diagrams, a map, etc.
Solution demo site
Basic solutions that can be flexibly adapted for yourself - change the appearance, business logic and even the structure of the database.
Discuss the project
Ask the initial questions about the project that concern you right now. We will advise you for free and recommend the best solution.