Skip to content

Instantly share code, notes, and snippets.

@cybersholt
Created February 10, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersholt/fcbca959c0049e26c354527dbfc4897f to your computer and use it in GitHub Desktop.
Save cybersholt/fcbca959c0049e26c354527dbfc4897f to your computer and use it in GitHub Desktop.
A MySQL routine to extract query strings from a given column, usage fn_getparam('PARAM',COLUMN)
DELIMITER $$
CREATE FUNCTION `fn_getparam`(param varchar(55), url varchar(2048)) RETURNS varchar(2048) CHARSET latin1 COLLATE latin1_general_cs
BEGIN
declare val varchar(2048);
declare _param varchar(60) DEFAULT CONCAT(param,'=');
select
case
when locate(concat('&',_param), url) > 0
then right(url, length(url) - (locate(concat('&',_param),url)+length(concat('&',_param))-1))
when locate(concat('?',_param), url) > 0
then right(url, length(url) - (locate(concat('?',_param),url)+length(concat('?',_param))-1))
when locate(concat('#',_param), url) > 0
then right(url, length(url) - (locate(concat('#',_param),url)+length(concat('#',_param))-1))
when locate(_param,url) > 0
then right(url, length(url) - (locate(_param,url)+length(_param)-1) )
else null
end
into val;
set val = replace(replace(left(val, locate('&',concat(val,'&'))-1),'%20',' '),'+',' ');
RETURN val;
END $$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment