0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

T-SQL: 文字列を指定文字で分離する

0
Last updated at Posted at 2019-09-30

SAMPLE

image.png

REFERENCE

Split a string to a table using T-SQL

CREATE FUNCTION

CREATE FUNCTION [dbo].[Split]
(
    @string NVARCHAR(2000),
    @delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
    WITH SPLIT(stpos,endpos)
    AS(
        SELECT 0 AS stpos, CHARINDEX(@delimiter,@string) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@delimiter,@string,endpos+1)
            FROM SPLIT
            WHERE endpos > 0
    )
    SELECT 'data' = SUBSTRING(@string,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM SPLIT
)

HOW TO USE

DECLARE @text NVARCHAR(100) = 'XXXXX/YYYYY/WWWWW'
SELECT data
FROM dbo.SPLIT(@text, '/')
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?