Skip to content

Commit 895e221

Browse files
committed
Add Remove_Leading_and_Trailing_Backward_and_Forward_Slashes script
1 parent 85d1b28 commit 895e221

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
https://sqlperformance.com/2019/10/t-sql-queries/overlooked-t-sql-gems
3+
*/
4+
5+
SELECT string, leading_slash_count, trailing_slash_count,
6+
STUFF(STUFF(string, LEN(string) - trailing_slash_count + 1,
7+
trailing_slash_count, ''), 1, leading_slash_count, '') AS new_string
8+
FROM ( VALUES
9+
(CAST('//\\ remove leading and trailing backward (\) and forward (/) slashes \\//' AS varchar(200))),
10+
('//\\**remove leading and trailing backward (\) and forward (/) slashes**\\//')
11+
) AS test_data(string)
12+
CROSS APPLY (
13+
SELECT PATINDEX('%[^/\]%', string) - 1 AS leading_slash_count,
14+
PATINDEX('%[^/\]%', REVERSE(string)) - 1 AS trailing_slash_count
15+
) AS ca1;

0 commit comments

Comments
 (0)