-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlserver2022-tsql-trim.sql
98 lines (65 loc) · 1.6 KB
/
sqlserver2022-tsql-trim.sql
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
SQL Server 2022
- funkcja TRIM, LTRIM, RTRIM
https://www.kursysql.pl
*/
USE AdventureWorks2019
-- przed SQL Server 2017
SELECT LTRIM(RTRIM( ' test '))
/*
TRIM
*/
-- SQL Server 2017-2019
-- TRIM ( [ characters FROM ] string )
-- usuwanie spacji
SELECT ' test '
SELECT TRIM ( ' test ')
SELECT TRIM ( ' ' FROM ' test ' )
-- usuwanie nie tylko spacji
SELECT TRIM ( '#' FROM '##test###' )
SELECT TRIM ( '#' FROM ' # test # ' )
-- usuwanie spacji i #
SELECT TRIM ( ' #' FROM ' # test # ' )
-- SQL Server 2022
-- TRIM ( [ LEADING | TRAILING | BOTH ] [characters FROM ] string )
SELECT TRIM ( LEADING ' #' FROM ' # test # ' )
SELECT TRIM ( TRAILING ' #' FROM ' # test # ' )
SELECT TRIM ( BOTH ' #' FROM ' # test # ' )
-- compat level min 160
ALTER DATABASE AdventureWorks2019 SET COMPATIBILITY_LEVEL = 150
GO
SELECT TRIM ( ' #' FROM ' # test # ' )
GO
ALTER DATABASE AdventureWorks2019 SET COMPATIBILITY_LEVEL = 160
GO
/*
LTRIM
*/
-- SQL Server 2019
-- LTRIM ( character_expression )
SELECT ' # test # '
UNION ALL
SELECT LTRIM ( ' # test # ' )
-- SQL Server 2022
-- LTRIM ( character_expression , [ characters ] )
SELECT ' # test # '
UNION ALL
SELECT LTRIM ( ' # test # ' , '#')
UNION ALL
SELECT LTRIM ( ' # test # ' , ' #')
/*
RTRIM
*/
-- SQL Server 2019
-- RTRIM ( character_expression )
SELECT ' # test # '
UNION ALL
SELECT RTRIM ( ' # test # ' )
-- SQL Server 2022
-- RTRIM ( character_expression , [ characters ] )
SELECT ' # test # '
UNION ALL
SELECT RTRIM ( ' # test # ' , '#')
UNION ALL
SELECT RTRIM ( ' # test # ' , ' #')