Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server detect other browsers from navigator.userAgent

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.

like image 522
Ali Soltani Avatar asked May 23 '18 11:05

Ali Soltani


People also ask

Is userAgent deprecated?

# Last Version for Unreduced User-Agent String platform DOM APIs). The origin trial that allowed sites to test the fully reduced User-Agent will end on April 19, 2022. After that date, the User-Agent String will be gradually reduced.

What is userAgent in Navigator?

The Navigator userAgent property is used for returning the user-agent header's value sent to the server by the browser. It returns a string representing values such as the name, version, and platform of the browser.

How do I fix page or script is accessing at least one of Navigator userAgent?

To fix this issue, replace the usage of navigator. userAgent, navigator. appVersion, and navigator. platform with feature detection, progressive enhancement, or migrate to navigator.

How can you detect the client's browser name JavaScript?

Answer: To establish the actual name of the user's Web browser, you can use the navigator. appName and navigator. userAgent properties. The userAgent property is more reliable than appName because, for example, Firefox or Opera may return the string "Netscape" as the value of navigator.


1 Answers

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.

like image 152
mson Avatar answered Nov 14 '22 11:11

mson