sql split string by delimiter code example
Example 1: create table split string function in sql server
SELECT
first_name,
last_name,
value phone
FROM
sales.contacts
CROSS APPLY STRING_SPLIT(phones, ',');
Example 2: split string by comma in sql server
CREATE FUNCTION Split
(
@delimited nvarchar(max),
@delimiter nvarchar(100)
) RETURNS @t TABLE
(
id int identity(1,1),
val nvarchar(max)
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'
insert into @t(val)
select
r.value('.','varchar(max)') as item
from @xml.nodes('//root/r') as records(r)
RETURN
END
GO
Example 3: sql split string by space
SELECT SUBSTRING('please notify the sender at the e-mail address above',0,30 +
charindex(' ',SUBSTRING('please notify the sender at the e-mail address above',31,len('please notify the sender at the e-mail address above')))
)
as part1,
SUBSTRING('please notify the sender at the e-mail address above',30+ charindex(' ',SUBSTRING('please notify the sender at the e-mail address above',31,len('please notify the sender at the e-mail address above'))),len('please notify the sender at the e-mail address above')) as part2