There was a column where URL's were stored. Some rows had one URL, some had 2 and some had more. Some were concatenated using delimiter spaces and some were pipe ( | ) and some were with comma. There was no fixed delimiter as in below image.
Firstly I needed to filter URL column having two URLs. One of my colleague helped me to filter it.
Filter containing two URL
Similarly we can filter data having three URL's
Next task was dynamic. I needed to return no of URL's available in each row. This is not just wordcount task. This is specified-repeated-wordcount task. So this T-SQL script helped me.
Firstly I needed to filter URL column having two URLs. One of my colleague helped me to filter it.
Filter containing two URL
SELECT URL from TableName where URL like '%www%www%'
Similarly we can filter data having three URL's
SELECT URL from TableName where URL like '%www%www%www%'
Next task was dynamic. I needed to return no of URL's available in each row. This is not just wordcount task. This is specified-repeated-wordcount task. So this T-SQL script helped me.
-------------------------------------------------------------------------------------------
-- Description : Shows specified string repeated count in each Cell
-- Uasage : Run Below commented query
/*
;With cte
As
(
Select 1 As ID,'abc' As [Description] Union all
Select 2 As ID,'abc xyz abc' As [Description] Union all
Select 2 As ID,'abc xyz abcabc' As [Description]
)
select [Description],dbo.WordRepeatedNumTimes([Description],'abc') As RepeatedWordCount from cte
*/
-------------------------------------------------------------------------------------------
CREATE function [dbo].[WordRepeatedNumTimes]
(@SourceString varchar(8000),@TargetWord varchar(8000))
RETURNS int
AS
BEGIN
DECLARE @NumTimesRepeated int
,@CurrentStringPosition int
,@LengthOfString int
,@PatternStartsAtPosition int
,@LengthOfTargetWord int
,@NewSourceString varchar(8000)
SET @LengthOfTargetWord = len(@TargetWord)
SET @LengthOfString = len(@SourceString)
SET @NumTimesRepeated = 0
SET @CurrentStringPosition = 0
SET @PatternStartsAtPosition = 0
SET @NewSourceString = @SourceString
WHILE len(@NewSourceString) >= @LengthOfTargetWord
BEGIN
SET @PatternStartsAtPosition = CHARINDEX
(@TargetWord,@NewSourceString)
IF @PatternStartsAtPosition <> 0
BEGIN
SET @NumTimesRepeated = @NumTimesRepeated + 1
SET @CurrentStringPosition = @CurrentStringPosition + @PatternStartsAtPosition +
@LengthOfTargetWord
SET @NewSourceString = substring(@NewSourceString,
@PatternStartsAtPosition +
@LengthOfTargetWord, @LengthOfString)
END
ELSE
BEGIN
SET @NewSourceString = ''
END
END
RETURN @NumTimesRepeated
END
-- Description : Shows specified string repeated count in each Cell
-- Uasage : Run Below commented query
/*
;With cte
As
(
Select 1 As ID,'abc' As [Description] Union all
Select 2 As ID,'abc xyz abc' As [Description] Union all
Select 2 As ID,'abc xyz abcabc' As [Description]
)
select [Description],dbo.WordRepeatedNumTimes([Description],'abc') As RepeatedWordCount from cte
*/
-------------------------------------------------------------------------------------------
CREATE function [dbo].[WordRepeatedNumTimes]
(@SourceString varchar(8000),@TargetWord varchar(8000))
RETURNS int
AS
BEGIN
DECLARE @NumTimesRepeated int
,@CurrentStringPosition int
,@LengthOfString int
,@PatternStartsAtPosition int
,@LengthOfTargetWord int
,@NewSourceString varchar(8000)
SET @LengthOfTargetWord = len(@TargetWord)
SET @LengthOfString = len(@SourceString)
SET @NumTimesRepeated = 0
SET @CurrentStringPosition = 0
SET @PatternStartsAtPosition = 0
SET @NewSourceString = @SourceString
WHILE len(@NewSourceString) >= @LengthOfTargetWord
BEGIN
SET @PatternStartsAtPosition = CHARINDEX
(@TargetWord,@NewSourceString)
IF @PatternStartsAtPosition <> 0
BEGIN
SET @NumTimesRepeated = @NumTimesRepeated + 1
SET @CurrentStringPosition = @CurrentStringPosition + @PatternStartsAtPosition +
@LengthOfTargetWord
SET @NewSourceString = substring(@NewSourceString,
@PatternStartsAtPosition +
@LengthOfTargetWord, @LengthOfString)
END
ELSE
BEGIN
SET @NewSourceString = ''
END
END
RETURN @NumTimesRepeated
END
No comments:
Post a Comment