Solution for SQL Server 2008:
here is the table:
CREATE TABLE MyTable (id INT, txt VARCHAR(MAX));
here is SQL query:
SELECT sum(case when TSplitted.txt_word = 'searched' then 1 else 0 end) as cnt_searched
, count(*) as cnt_all
FROM MyTable MYT
INNER JOIN Fn_Split(MYT.id,' ',MYT.txt) TSplitted on MYT.id=TSplitted.id
here is table valued function Fn_Split(@id int, @separator VARCHAR(32), @string VARCHAR(MAX)) (taken from here):
CREATE FUNCTION Fn_Split (@id int, @separator VARCHAR(32), @string VARCHAR(MAX))
RETURNS @t TABLE
(
ret_id INT
,txt_word VARCHAR(MAX)
)
AS
BEGIN
DECLARE @xml XML
SET @XML = N'<root><r>' + REPLACE(@s, @separator, '</r><r>') + '</r></root>'
INSERT INTO @t(ret_id, val)
SELECT @id, r.value('.','VARCHAR(5)') as Item
FROM @xml.nodes('//root/r') AS RECORDS(r)
RETURN
END