-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbo.udf_string_urlquerystring.sql
More file actions
77 lines (62 loc) · 4.49 KB
/
Copy pathdbo.udf_string_urlquerystring.sql
File metadata and controls
77 lines (62 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
USE [NBA]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*------------------------------------------------------------------------------
Function Name: [dbo].[udf_string_urlquerystring]
Purpose: Extracts the query string portion from a URL
Project: T-SQL-Scripts
Date Created: 2025-10-14
Author: Ludwig Mark
Parameters:
@url - VARCHAR(2600) - The URL to extract query string from
Returns: NVARCHAR(MAX)
Notes:
- Returns everything after the first ? character
- Preserves encoded characters
- Handles multiple ? characters correctly
- Thread-safe and deterministic
- Maximum URL length matches Windows MAX_PATH
Performance:
- Execution time: O(n) where n is URL length
- Memory usage: Minimal, uses string operations
- Single pass through the string
Dependencies:
- Requires STRING_SPLIT function (SQL Server 2016+)
Example:
------------------------
DECLARE @URL VARCHAR(2600) = 'https://example.com/path?name=value&id=123'
SELECT dbo.udf_string_urlquerystring(@URL) AS QueryString
Returns: 'name=value&id=123'
Modification History:
Date Author Description
---------- ---------- ------------------------------------------------
2025-10-14 Ludwig M. Initial creation
Validation:
- Returns NULL if URL is NULL
- Returns NULL if no query string exists
- Preserves all characters after ?
- Handles encoded query parameters
- Works with fragment identifiers (#)
Query String Format:
- Starts with ? character (removed in output)
- Parameters in key=value format
- Multiple parameters joined by &
- Special characters may be URL-encoded
LICENSE: MIT
------------------------------------------------------------------------------*/
CREATE FUNCTION [dbo].[udf_string_urlquerystring](
@url VARCHAR(2600)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN (
SELECT SUBSTRING(@url, LEN(value) + 2, LEN(@url) - LEN(value))
FROM STRING_SPLIT(@url + '?', '?')
WHERE SUBSTRING(@url, LEN(value) + 1, LEN(@url) - LEN(value)) LIKE '?%'
)
END
GO