Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
SQL Split Function
WhatsApp
Ashish Srivastava
May 07
2016
752
0
1
CREATE
FUNCTION
[dbo].[SplitString]
(
@DelimitedString
varchar
(8000),
@Delimiter
varchar
(100)
)
RETURNS
@tblArray
TABLE
(
Element
varchar
(1000)
-- Array element contents
)
AS
BEGIN
-- Local Variable Declarations
-- ---------------------------
DECLARE
@
Index
smallint
,
@Start
smallint
,
@DelSize
smallint
SET
@DelSize = LEN(@Delimiter)
-- Loop through source string and add elements to destination table array
-- ----------------------------------------------------------------------
WHILE LEN(@DelimitedString) > 0
BEGIN
SET
@
Index
= CHARINDEX(@Delimiter, @DelimitedString)
IF @
Index
= 0
BEGIN
INSERT
INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(@DelimitedString)))
BREAK
END
ELSE
BEGIN
INSERT
INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(
SUBSTRING
(@DelimitedString, 1,@
Index
- 1))))
SET
@Start = @
Index
+ @DelSize
SET
@DelimitedString =
SUBSTRING
(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1)
END
END
RETURN
END
GO
SELECT
*
FROM
dbo.SplitString (
'10:50'
,
':'
)
Output:
Element
10
50
SQL
Split Function
Up Next
SQL Split Function