1

I have a table with an ID and a nvarchar field.

In each line I have something like this:

ID   ClientID    TEXT
6       1          'Log Entry A' - 26/05/2014 17:32:30; - UserName 'Log Entry B' - 27/05/2014 18:30:30; - UserName
7       2          'Log Entry 2A' - 27/05/2014 17:32:30; - UserName 'Log Entry 2B' - 27/05/2014 18:32:30; - UserName

The TEXT field here is a NVARCHAR column that holds all the Client-related user activity on a given day (e.g.: all the user activity from the user UserName on the day 27/05/2014)

The problem is: I need to figure out how many times the user "UserName" had an activity logged on on each line.

Until now, I figured that using something like this:

(...) WHERE TEXT LIKE '%27/05/2014_____________UserName%'

I can ignore the hour and grab all the columns that have the date I want. However, this don't solve my problem: this just give if the user was logged on or not, but I still need to know how many times the user was logged on that day.

What I wanted from this is something similar to this:

SELECT ID, CountSubstrings(TEXT, '%27/05/2014_____________UserName%') as Count, TEXT
FROM ClientData WHERE TEXT LIKE '%27/05/2014_____________UserName%' 

Which would give me this as a result:

ID    Count   TEXT
 6    1   'Log Entry A' - 26/05/2014 17:32:30; - UserName 'Log Entry B' - 27/05/2014 18:30:30; - UserName
 7    2   'Log Entry 2A' - 27/05/2014 17:32:30; - UserName 'Log Entry 2B' - 27/05/2014 18:32:30; - UserName

Any Ideas?

0

3 Answers 3

1

Here is one approach, assuming the username is surrounded by spaces:

select t.*,
       ((len(' ' + text + ' ') -
         len(replace(' ' + text + ' ', ' ' + @UserName + ' ', ''))
        ) / (2 + len(@UserName)) -- the "2" is for the spaces at the beginning and end
       ) as NumberOfTimes 
from table t;
1
  • Hmm... No, this is not really what I need. I need to know the number of times a user went on activity on a given day. If a Line holds 3 user activities, but only two on the "filter-day", then "NumberOfTimes" should return 2, not 3. This will return 3.
    – T. Sar
    Jun 30, 2014 at 20:02
1

SQL Fiddle Demo

DECLARE @SearchFilter nvarchar(max) = '%27/05/2014%'

SELECT
  [ClientID],
  COUNT(*) [count]
FROM ClientData t1
CROSS APPLY(SELECT CAST('<a>'+REPLACE([TEXT],';','</a><a>')+'</a>' AS xml)) t2(xml)
CROSS APPLY xml.nodes('a') t3(log_entry)
WHERE log_entry.value('.','nvarchar(max)') LIKE @SearchFilter
GROUP BY ClientID
4
  • Can you explain why this works? I'm not familiar with the XML functions. Jun 30, 2014 at 21:28
  • Look at the output of SELECT <a>'+REPLACE([TEXT],';','</a><a>')+'</a>' FROM ClientData
    – Anon
    Jun 30, 2014 at 21:36
  • @Anon Well. I put this on my server and tried to run. It is giving me a message saying that "log_entry can't be used directly".
    – T. Sar
    Jul 1, 2014 at 10:41
  • @ThalesPereira use explicit column names instead of SELECT *
    – Anon
    Jul 1, 2014 at 14:43
0

A clean way would be to code an SQL scalar-valued user defined function that returns the count of the substring you want to find in the provided string (a function that takes 2 parameters and does the count).

1
  • Tried that. I wasn't able to do a user-defined function that works right with Wildcards...
    – T. Sar
    Jun 30, 2014 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.