-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-SQL-String-Function
More file actions
102 lines (47 loc) · 1.93 KB
/
16-SQL-String-Function
File metadata and controls
102 lines (47 loc) · 1.93 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
SQL String Functions
SQL provides various string functions that allow you to manipulate and work with string data. Here are some commonly used SQL string functions:
1️⃣ CHARACTER_LENGTH()
This function returns the number of characters in a string.
2️⃣ UPPER()
This converts text to uppercase.
3️⃣ LOWER()
This converts text to lowercase.
4️⃣ LEFT()
This returns characters from the left side of a string.
5️⃣ RIGHT()
This returns characters from the right side of a string.
6️⃣ SUBSTRING()
Extracts part of a string.
7️⃣ CONCAT()
This joins multiple strings together.
USE market_star_schema;
-- find the character length of the string "Data Analysis"
SELECT CHARACTER_LENGTH("Data Analysis");
-- find the minimum and maximum character lengths of customer name
SELECT MIN(CHARACTER_LENGTH(customer_name)), MAX(CHARACTER_LENGTH(customer_name))
FROM cust_dimen;
-- convert the customer name to uppercase and lowercase
SELECT customer_name,
UPPER(customer_name), LOWER(customer_name)
FROM cust_dimen;
-- find the first 3 characters of the city name.
SELECT LEFT("Hyderabad",3);
-- find the city name that Start from position 2 Extract 3 characters
SELECT city, SUBSTRING(city, 2, 3)
FROM cust_dimen;
-- find the city name that start from position 2 Extract all characters
SELECT city, SUBSTRING(city,2)
FROM cust_dimen;
-- find the city name that start from position -3 EXtract 4 characters
SELECT city, SUBSTRING(city,-3,4)
FROM cust_dimen;
-- concatenate the stings "Data Analysis" and "!" with a space in between
SELECT CONCAT("Data"," ","Analysis"," !");
-- concatenate the city and state with a hyphen('-') in between
SELECT city, state, CONCAT(city, "-",state)
FROM cust_dimen;
-- concat the first three letters and last three letters of state
SELECT state,
CONCAT(LEFT(state,3),RIGHT(state,3)) AS result,
CHARACTER_LENGTH(CONCAT(LEFT(state,3),RIGHT(state,3))) AS count
FROM cust_dimen;