Created
June 17, 2019 13:24
-
-
Save dmlogv/81a4673ccfb0bd2b86f5f11be4462343 to your computer and use it in GitHub Desktop.
Count substrings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Counts a number of substrings in the given string | |
Args: | |
@string | |
@substring | |
Returns: | |
INT | |
Examples: | |
select dbo.fncCountSubstrings('бацацацацацацаха', 'ац') | |
-> 6 | |
*/ | |
CREATE OR ALTER FUNCTION [dbo].[fncCountSubstrings] | |
( | |
@string NVARCHAR(max), | |
@substring NVARCHAR(100) | |
) | |
RETURNS INT | |
AS | |
BEGIN | |
IF LEN(@substring) = 0 RETURN 0 | |
DECLARE @result INT = ( LEN(@string) - LEN(REPLACE(@string, @substring, '')) ) / LEN(@substring) | |
RETURN @result | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment