"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Achieve MySQL's SUBSTRING_INDEX Functionality in SQL Server 2012?

How to Achieve MySQL's SUBSTRING_INDEX Functionality in SQL Server 2012?

Published on 2024-12-21
Browse:201

How to Achieve MySQL's SUBSTRING_INDEX Functionality in SQL Server 2012?

SQL Server Equivalent of substring_index Function in MySQL

The substring_index function in MySQL is used to extract a substring from a specified string before a given number of occurrences of a delimiter. To achieve similar functionality in SQL Server 2012, you can employ one of the following approaches:

T-SQL Scalar Function:

CREATE FUNCTION dbo.SUBSTRING_INDEX
(
    @str NVARCHAR(4000),
    @delim NVARCHAR(1),
    @count INT
)
RETURNS NVARCHAR(4000)
WITH SCHEMABINDING
BEGIN
    -- Convert the string to an XML document for processing
    DECLARE @XmlSourceString XML;
    SET @XmlSourceString = (SELECT N'<root><row>'   REPLACE( (SELECT @str AS '*' FOR XML PATH('')) , @delim, N'</row><row>' )   N'</row></root>');

    -- Extract the desired substring using XQuery
    RETURN STUFF
    (
        ((
            SELECT  @delim   x.XmlCol.value(N'(text())[1]', N'NVARCHAR(4000)') AS '*'
            FROM    @XmlSourceString.nodes(N'(root/row)[position() <= sql:variable("@count")]') x(XmlCol)
            FOR XML PATH(N''), TYPE
        ).value(N'.', N'NVARCHAR(4000)')), 
        1, 1, N''
    );
END
GO

-- Example usage
SELECT dbo.SUBSTRING_INDEX(N'www.somewebsite.com', N'.', 2) AS Result;

T-SQL Inline Table-Valued Function:

CREATE FUNCTION dbo.SUBSTRING_INDEX
(
    @str NVARCHAR(4000),
    @delim NVARCHAR(1),
    @count INT
)
RETURNS TABLE
AS 
RETURN
    WITH Base
    AS 
    (
        SELECT XmlSourceString = CONVERT(XML, (SELECT N'<root><row>'   REPLACE( (SELECT @str AS '*' FOR XML PATH('')) , @delim, N'</row><row>' )   N'</row></root>'))
    )   
    SELECT STUFF
    (
        ((
            SELECT  @delim   x.XmlCol.value(N'(text())[1]', N'NVARCHAR(4000)') AS '*'
            FROM    Base b 
            CROSS APPLY b.XmlSourceString.nodes(N'(root/row)[position() <= sql:variable("@count")]') x(XmlCol)
            FOR XML PATH(N''), TYPE
        ).value(N'.', N'NVARCHAR(4000)')), 
        1, 1, N''
    ) AS Result;
GO

-- Example usage
SELECT  *
FROM    (
    SELECT N'www.somewebsite.com' UNION ALL 
    SELECT N'www.yahoo.com' UNION ALL 
    SELECT N'www.outlook.com'
) a(Value)
CROSS APPLY dbo.SUBSTRING_INDEX(a.Value, N'.', 2) b;

These functions allow you to replicate the behavior of the MySQL substring_index function in SQL Server, enabling you to portably perform substring extraction tasks.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3