-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathString functions.sql
More file actions
40 lines (30 loc) · 1.36 KB
/
String functions.sql
File metadata and controls
40 lines (30 loc) · 1.36 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
-- CONCAT() returns concatenated string
SELECT CONCAT('abc', 'def');
SELECT CONCAT('abc', ' ', 'def');
-- LEFT() returns the leftmost number of characters as specified
SELECT LEFT('abcerfgh',4);
-- LENGTH() returns the length of a string
SELECT LENGTH('abcerfgh');
-- LOWER() returns the string in lowercase
SELECT LOWER('QSDFGH');
-- REPLACE() is used to replace a string in a a sentence
SELECT REPLACE('Data Science', 'Science', 'Analysis');
-- REVERSE() reverses the order of characters in a string
SELECT REVERSE('Data Science');
-- RIGHT() returns the specified rightmost number of characters
SELECT RIGHT('abcerfgh', 2);
/*
STRCMP() compares two strings, by definition from MySQL doc:
returns 0 if the strings are the same, -1 if the first argument is smaller than the second
according to the current sort order, and NULL if either argument is NULL. It returns 1 otherwise.
*/
SELECT STRCMP('date','data');
SELECT STRCMP(null,null);
-- SUBSTR() or SUBSTRING() returns the substring of a string starting from the defined position
SELECT SUBSTR('data science', 5);
-- TRIM() removes leading and trailing spaces, and the specified characters from both sides
SELECT (' abcerfgh ') AS result;
SELECT TRIM(' abcerfgh') AS result;
SELECT TRIM(BOTH 'z' FROM 'zzzzzzzabcerfghzzzzzzz') AS result;
-- UPPER() returns the string in uppercase
SELECT UPPER('abc');