SQL Server - split function

2

I have the following table:

The purpose is to do a function of the EixoX column in SQL Server where TipoGrafico is different from 'StockChart' >. That is, if TipoGrafico is different from 'StockChart' then I should split the contents of the EixoX column.

Does anyone have an idea how to do it?

Thanks in advance!)

    
asked by anonymous 27.07.2018 / 11:52

1 answer

1

I have a function ready for you.

CREATE FUNCTION [dbo].[f_split] (@String NVARCHAR(4000), @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 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM Split
)
    
27.07.2018 / 14:07