9

I have a table like below:

create table SiteLog (UserAgent nvarchar(255))
insert into SiteLog values 
('Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1'),
('Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'),
('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'),
('Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'),
('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.12 Safari/537.36 OPR/14.0.1116.4') 

UserAgent was filled by navigator.userAgent client users. I want to categorize records to 6 categories like below:

  1. Firefox
  2. Edge
  3. IE
  4. Chrome
  5. Safari
  6. Other

I tried to do it like below:

SELECT CASE 
         WHEN Charindex('Firefox', useragent) > 0 THEN 'Firefox' 
         WHEN Charindex('Edge', useragent) > 0 THEN 'Edge' 
         WHEN Charindex('Trident', useragent) > 0 
               OR Charindex('MSIE', useragent) > 0 THEN 'IE' 
         WHEN Charindex('Chrome', useragent) > 0 THEN 'Chrome' 
         WHEN Charindex('Safari', useragent) > 0 THEN 'Safari' 
         ELSE 'Other' 
       END 
FROM   SiteLog 

At first glance, it is very easy, but as you can see in sqlfiddle, the result is not true because the last record has Chrome and it is categorized in Chrome category. I spent too much time to figure out a solution for doing it, but I couldn't.

Edit

I can't use CLR Assembly RegEx Functions

It would be very helpful if someone could explain a solution for this issue.

4
  • 1
    Switch the Chrome and Safari WHEN expressions around? Is SQL Server really the right tool for this though? I'm pretty sure there are tools out there specifically designed to read and provide analysis on web log files.
    – Thom A
    Commented May 23, 2018 at 11:21
  • 1
    Whats the rule to define your category? The 3rd line has Chrome, Safari and Edge. First think of your desired behavior then move on to the implementation.
    – EzLo
    Commented May 23, 2018 at 11:32
  • @Larnu Switching the Chrome and Safari doesn't work in all situations. You are right another tools fill this table and I know SQL Server isn't suitable tools for doing that, but unfortunately I just have a table like this. Commented May 23, 2018 at 11:39
  • @EzLo I thought about my code and I saw many patterns about useragent. I arranged code lines according to useragent patterns. I can categorize them in JS or C# with Regex, but it in case, my data is just this table and I should do it in SQL Server. Commented May 23, 2018 at 11:47

3 Answers 3

1

The technical answer to your issue is that in a SQL case statement, the first response that hits true escapes the rest of the condition checks. You could check the conditions in the most restrictive to least restrictive order and you'll be able to get the output you want.

However, it is not a great solution. User Agent is not reliable or predictable - also there are a whole lot of them https://udger.com/resources/ua-list.

You have another issue that you are doing a search in the middle of the string. This will make this query very expensive and it will be very slow once you hit substantial records.

It would be better to parse the user agent while you are inserting the record so that when you do searches/counts, it can be optimized to use indices.

0

I've made some assumptions on what your expected output is:

SELECT 
    SL.*
    ,a.a + b.b + c.c + d.d + e.e + z.z -- Format as you like
FROM   SiteLog SL
            CROSS APPLY (SELECT CASE WHEN Charindex('Firefox', useragent) > 0 THEN ' Firefox' ELSE '' END) a(a)
            CROSS APPLY (SELECT CASE WHEN Charindex('Edge', useragent) > 0 THEN ' Edge'  ELSE '' END) b(b)
            CROSS APPLY (SELECT CASE WHEN Charindex('Trident', useragent) > 0  OR Charindex('MSIE', useragent) > 0 THEN ' IE' ELSE '' END) c(c)
            CROSS APPLY (SELECT CASE WHEN Charindex('Chrome', useragent) > 0 THEN ' Chrome'  ELSE '' END) d(d)
            CROSS APPLY (SELECT CASE WHEN Charindex('Safari', useragent) > 0 THEN ' Safari'  ELSE '' END) e(e)
            CROSS APPLY (SELECT CASE WHEN a.a + b.b + c.c + d.d + e.e = '' THEN ' Other' ELSE '' END) z(z)
0

This worked for me. You should be able to pass your USER agent data and replace it with this

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52

SQL Code:

SELECT 
CASE
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Mac%OS%' THEN 'Mac OS X'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%iPad%' THEN 'iPad'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%iPod%' THEN 'iPod'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%iPhone%' THEN 'iPhone'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%imac%' THEN 'mac'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%android%' THEN 'android'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%linux%' THEN 'linux'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Nokia%' THEN 'Nokia'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%BlackBerry%' THEN 'BlackBerry'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%win%' THEN
        CASE
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 6.2%' THEN 'Windows 8'
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 6.3%' THEN 'Windows 8.1'
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 6.1%' THEN 'Windows 7'
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 6.0%' THEN 'Windows Vista'
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 5.1%' THEN 'Windows XP'
            WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NT 5.0%' THEN 'Windows 2000'
            ELSE 'Windows'
        END      
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%FreeBSD%' THEN 'FreeBSD'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%OpenBSD%' THEN 'OpenBSD'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%NetBSD%' THEN 'NetBSD'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%OpenSolaris%' THEN 'OpenSolaris'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%SunOS%' THEN 'SunOS'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%OS/2%' THEN 'OS/2'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%BeOS%' THEN 'BeOS'
    ELSE 'Unknown'
END AS 'os',
CASE
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%edge%'THEN 'Edge'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%MSIE%' THEN 'Internet Explorer'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Firefox%' THEN 'Mozilla Firefox'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Chrome%' THEN 'Google Chrome'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Safari%' THEN 'Apple Safari'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Opera%' THEN 'Opera' 
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%Outlook%' THEN 'Outlook' 
    ELSE 'Unknown'
END AS 'Browser',
CASE
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%WOW64%' THEN '64 bit'
    WHEN 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.52' LIKE '%x64%' THEN '64 bit'
    ELSE '32 bit'
END AS 'BitArchitecture'

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.