Repository navigation
Expand file tree
/
Copy path10_SQL_String-functions.sql
More file actions
139 lines (103 loc) · 2.92 KB
/
Copy path10_SQL_String-functions.sql
File metadata and controls
139 lines (103 loc) · 2.92 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
-------------String Functions---------------
/*
Typy string functions:
- manipulation (concat,upper,lower,trim,replace)
- calculation (len)
- string extraction (left, right, substring)
*/
-------------CONCAT------------
/*
Laczy wiele stringow w jeden.
*/
USE SalesDB
/*task: Concatenate first name and country into one column*/
SELECT
CONCAT(FirstName, ' ' ,Country) AS concatenation
FROM Sales.Customers
------------UPPER i LOWER------------
/*
UPPER zmienia litery na wielkie, LOWER na male
*/
/*task: Convert the first name to lowercase or uppercase */
SELECT
LOWER(FirstName) as male_imiona,
UPPER(LastName) as duze_nazwiska
FROM Sales.Customers
----------------TRIM---------------
/*
Usuwa spacje na poczatku i na koncu stringa.
*/
/*task: Find customers whose first name contains leading or trailing spaces*/
USE MyDatabase
SELECT
TRIM(first_name) as bez_spacji
FROM dbo.customers
EXCEPT
SELECT
first_name
FROM dbo.customers
--lub
SELECT
first_name,
LEN(first_name) as len_name, --- mozna wyswietlac ta sama kolumne wiele razy
LEN(TRIM(first_name)) as len_trim_name,
LEN(first_name) - LEN(TRIM(first_name)) as flag
FROM customers
--WHERE first_name != TRIM(first_name)
---------------REPLACE--------------
/*
Zamienia konktretny znak nowym znakiem lub usuwa znak (wtedy dajemy '').
*/
/*task: Remove dashes (-) from a phone number*/
SELECT
'123-456-678' as numer_telefonu,
REPLACE('123-456-678','-','');
SELECT
'report.txt' as txt,
REPLACE('report.txt','.txt','.pdf') as pdf;
/*
SELECT
CustomerID,
Phone,
REPLACE(Phone, '-', ' ') AS FormattedPhone
FROM Customers;
to zadziala tak ze kazdy numer telefonu z kolumny Phone zostanie sformatowany
*/
-----------------LEN-----------------
/*
Liczy ile znakow jest w danym stringu.
*/
/*task: Calculate the length of each customer's first name*/
SELECT
first_name,
LEN(TRIM(first_name)) as ile_liter -- jakbym nie dal trim to john by pokazalo ze ma 5 bo spacje tez sie licza
FROM dbo.customers
-------------LEFT i RIGHT--------------
/*
LEFT ekstraktuje podana liczbe znakow od poczatku stringa.
RIGHT ekstraktuje podana liczbe znakow od konca stringa.
*/
/*task: Retrieve the first two charakters of each first name*/
SELECT
first_name,
LEFT(TRIM(first_name),2) as pierwsze_dwie
FROM dbo.customers
/*task: Retrieve the last two characters of eacxh first name*/
SELECT
first_name,
RIGHT(TRIM(first_name),2) as ostatnie_dwie
FROM dbo.customers;
---------------SUBSTRING--------------
/*
Zwraca czesc stringa w okreslonej pozycji.
struktura: SUBSTRING(Value, Start, Lenght)
Uzywa sie tego do zwracania czesci stringow od okreslonego miejsca.
Jesli mamy za zadanie np wydobyc wszystkie znaki od drugiego znaku
to jako parametr Lenght uzywamy funkcji LEN(). (dynamiczna dlugosc)
*/
/*task: Retrieve a list of customers' first names after removing the
first character*/
SELECT
first_name,
SUBSTRING(TRIM(first_name),2,LEN(first_name)) as litery_po_1
FROM dbo.customers