Probably should be more specific about how you've gone about this so far, but anyway. Starting simple:
DECLARE @strItem [nvarchar](12)
SET @strItem = '%Yes%'
DECLARE @strInput [nvarchar](123)
SET @strInput = '~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~'
SELECT PATINDEX(@strItem, @strInput) As [whereinStr]
Gets:
whereinStr
~~~~~~~~~~
8
Since WHAT ("word") has to be known, usg PATINDEX in conjunction with some of the string operations (RIGHT, LEFT, SUBSTRING) might be appropriate. Here's some "Message Window" output stuff which looks interesting when, as a TSQL query, is executed:
DECLARE @strUnknown [nvarchar](900)
SET @strUnknown = '~~~~~~~Yes~~~~~~~~No~No~~~~~~~~~'
DECLARE @intSpecial [int]
SET @intSpecial = 0
DECLARE @strTemp [nvarchar](270)
SET @strTemp = 'Maybe~~~~~~~~No~No~~~~~~~~~'
DECLARE @intDelimit [int]
SET @intDelimit = PATINDEX('%~%',@strUnknown)
DECLARE @intDel [int]
SET @intDel = PATINDEX('%~%',@strTemp)
DECLARE @strEatIt [nvarchar](256)
SET @strEatIt = ''
DECLARE @intLoop [int]
SET @intLoop = 1
SET @intSpecial = LEN(@strUnknown)
WHILE @intLoop < @intSpecial
BEGIN
--PRINT @strUnknown
SET @strEatIt = (SELECT RIGHT(@strUnknown,LEN(@strUnknown)-1))
SET @strUnknown = @strEatIt
IF LEFT(@strUnknown,1) != '~'
PRINT 'to consume: ' + CAST(LEN(@strUnknown) AS [nvarchar]) + ' starting with this: ' + @strUnknown + ' the index is: ' + CAST(@intLoop AS [nvarchar])
SET @intLoop = @intLoop + 1
END
Gets:
to consume: 25 starting with this: Yes~~~~~~~~No~No~~~~~~~~~ the index is: 7
to consume: 24 starting with this: es~~~~~~~~No~No~~~~~~~~~ the index is: 8
to consume: 23 starting with this: s~~~~~~~~No~No~~~~~~~~~ the index is: 9
to consume: 14 starting with this: No~No~~~~~~~~~ the index is: 18
to consume: 13 starting with this: o~No~~~~~~~~~ the index is: 19
to consume: 11 starting with this: No~~~~~~~~~ the index is: 21
to consume: 10 starting with this: o~~~~~~~~~ the index is: 22
[edit]
Some more naive functionality which might be of interest:
CREATE FUNCTION fn_EZ_countWord
(
@strInput [nvarchar](1215),
@strWord [nvarchar](20)
)
RETURNS INT
AS
BEGIN
RETURN (LEN(@strInput)-LEN(REPLACE(@strInput,@strWord,'')))/LEN(@strWord)
END
Exercising:
DECLARE @strInput [nvarchar](123)
SET @strInput = 'Maybe~~~~~~~~No~No~~~~~~~~~'
SELECT [fn_EZ_SO_countOccurancesOfString](@strInput, 'Maybe') -- 1
SELECT [fn_EZ_SO_countOccurancesOfString](@strInput, '~') -- 18
[end edit]