Langsung ke konten utama

Split String On Table value fungtion SQL Server

Split String from string to row entry.


[sql]
CREATE FUNCTION [dbo].[SplitString](
@String NVARCHAR(8000)
,@Delimiter CHAR(1))
RETURNS TABLE
AS
RETURN(
WITH SplitData(
stpos
,endpos)
AS (
SELECT
0 AS stpos
,CHARINDEX(@Delimiter,@String) AS endpos
UNION ALL
SELECT
endpos + 1
,CHARINDEX(@Delimiter,@String,endpos+1)
FROM SplitData
WHERE endpos > 0)
SELECT
ROW_NUMBER() OVER(ORDER BY
(
SELECT
1
)) AS RowID
,RTRIM(LTRIM(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String) + 1)-stpos))) AS RowData
FROM SplitData)
[/sql]

Komentar

Postingan populer dari blog ini

sys.processes Status sp_who2 SQL Server

Taken from the books online reference for sys.processes  and the status column. dormant = SQL Server is resetting the session. running = The session is running one or more batches. When Multiple Active Result Sets (MARS) is enabled, a session can run multiple batches. For more information, see Using Multiple Active Result Sets (MARS). background = The session is running a background task, such as deadlock detection. rollback = The session has a transaction rollback in process. pending = The session is waiting for a worker thread to become available. runnable = The task in the session is in the runnable queue of a scheduler while waiting to get a time quantum. spinloop = The task in the session is waiting for a spinlock to become free. suspended = The session is waiting for an event, such as I/O, to complete.