-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_functions.sql
128 lines (104 loc) · 4.32 KB
/
string_functions.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
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
-- STRING FUNCTION
-- CONCAT - combine data for cleaner output
-- CONCAT(x,y,z,...)
SELECT CONCAT('h', 'e', 'l');
SELECT CONCAT(author_fname, ' ', author_lname) AS author_full_name
FROM books;
-- CONCAT_WS - CONCAT with seperator
-- CONCAT_WS('<seperator>', x,y,z,..)
SELECT CONCAT_WS('!', 'x', 'y','z');
SELECT CONCAT_WS(' ', author_fname, author_lname)
FROM books;
-- SUBSTRING - returns a smaller piece out of a larger string
-- SUBSTRING('<string>', starting_position, length_of_substring)
-- SUBSTRING('Hello', 1, 4) returns 'Hell'
-- if we don't provide the length_of_substring then it will go to the end
-- SUBSTRING('Hello World!', 7) returns 'World!'
SELECT SUBSTRING('Hello World!', 7);
-- we can also use negative indexing like in python
SELECT SUBSTRING('Hello World!', -5); -- returns 'orld!'
-- SUBSTR = SUBSTRING
SELECT SUBSTR(title, 1, 15)
FROM books;
SELECT SUBSTR(author_fname, 1, 1) AS initial, author_lname
FROM books;
-- combining CONCAT and SUBSTR
SELECT CONCAT(SUBSTR(title, 1, 10), '...') AS short_title
FROM books;
SELECT
CONCAT(SUBSTR(author_fname, 1, 1),
'.',
SUBSTR(author_lname, 1, 1),
'.') AS author_initials
FROM
books;
-- REPLACE - replacing parts of strings (in output)
-- REPLACE(str, from_str, to_str) case-sensitive!!!
SELECT REPLACE(title, ' ', '-')
FROM books;
-- REVERSE - returns a reversed string
SELECT REVERSE(title) FROM books;
SELECT CONCAT(author_fname, REVERSE(author_fname))
FROM books;
-- CHAR_LENGTH - returns number of characters in a given string
-- LENGTH - returns lenght of a string measured in bytes!!
SELECT CHAR_LENGTH(title) AS title_length, title
FROM books;
-- UPPER and LOWER - change casing of a given string
-- UCASE = UPPER, LCASE = LOWER
SELECT UPPER(title) FROM books;
-- 'I LOVE <title-upper> !!!'
SELECT CONCAT_WS(' ', 'I LOVE', UPPER(title), '!!!') AS opinion
FROM books;
-- LEFT(str, len)
-- Returns the leftmost len characters from the string str, or NULL if any argument is NULLL
-- RIGHT(str, len)
-- Returns the rightmost len characters from the string str, or NULL if any argument is NULL.
SELECT CONCAT(LEFT(author_fname, 1), '.', LEFT(author_lname, 1), '.') AS author_initials
FROM books;
-- INSERT(str,pos,len,newstr)
-- Returns the string str, with the substring beginning at position
-- pos and len characters long replaced by the string newstr
SELECT INSERT('Hello Bobby', 6, 0, ' There');
SELECT INSERT('Hello Bobby', 6, 2, ' There');
SELECT INSERT('World', 1, 2, 'Temp');
-- REPEAT(str,count)
-- Returns a string consisting of the string str repeated count times.
-- If count is less than 1, returns an empty string. Returns NULL if str or count is NULL
SELECT REPEAT('ha', 6);
-- TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)
-- Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH,
-- LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified,
-- spaces are removed.
SELECT TRIM(' text '); -- trimming leading and trailing spaces
SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx'); -- trimming leading 'x'
SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx'); -- trimming both leading and trailing 'x'
SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz'); -- trimming trailing 'xyz'
-- Reverse and Uppercase the sentence
-- Why does my cat look at me with such hatred?
SELECT REVERSE(UCASE('Why does my cat look at me with such hatred?'));
-- Replace spaces in titles with '->'
SELECT REPLACE(title, ' ', '->') AS title
FROM books;
-- Select lasts name of the authors and the backwards of them
SELECT author_lname AS forwards, REVERSE(author_lname) AS backwards
FROM books;
-- Select authors' full names in uppercase
SELECT UCASE(CONCAT_WS(' ', author_fname, author_lname)) AS full_name_in_caps
FROM books;
-- title + was released in + year of publication
SELECT CONCAT_WS(' ', title, 'was released in', released_year) AS blurb
FROM books;
-- Print book titles and the length of each title
SELECT title, CHAR_LENGTH(title) AS character_count
FROM books;
-- Print
-- short title (10 characters of the title + '...'
-- author as: <last_name>,<first_name>
-- quantity: <quantity> in stock
SELECT
CONCAT(LEFT(title, 10), '...') AS short_title,
CONCAT_WS(',', author_lname, author_fname) AS author,
CONCAT(stock_quantity, ' in stock') AS quantity
FROM
books;