Skip to content
Advertisement

Parse utm parameters in sql query

I have a table in my database that tracks visitors page views and I am trying to pull out the utm tracking parameters from the url

You can use this script to create table

This my script

I am getting funny results where if there is no utm tracking it is still populating fields and the utm_id is incorrect in most cases and when I run it on my big table I get the following error but I don’t have an example of a URL which is causing the error but is there a way I can add an if error to the cast statement?

Msg 537, Level 16, State 3, Line 3
Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.

Any help is appreciated.

Advertisement

Answer

Here is an option using string_split() in concert with a CROSS APPLY

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement