-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Math_Functions.sql
More file actions
116 lines (69 loc) · 2.99 KB
/
String_Math_Functions.sql
File metadata and controls
116 lines (69 loc) · 2.99 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
use sakila;
-- dual is a dummy table
-- STRING FUNCTIONS
-- lower() => convert upper case string to lower case
select first_name, lower(first_name) from actor;
-- upper() => convert lower case string to upper case
select upper("hello") from dual;
-- length() => gives length of a string in terms of bytes
select length('한') from dual; -- the korean character here takes 3 bytes for each character while in english, only 1 byte is used
-- char_length() => gives length in terms of count of characters
select char_length('한') from dual;
-- concat() => add two words or strings
select concat(first_name, " ", last_name, "hello") as full_name from actor;
-- concat_ws() => add two words or strings with a symbol as concatenating symbol
select concat_ws(" ", first_name, last_name, "hllo") as full_name from actor;
-- substr() => find a sub string from the a string from the given position
select first_name, substr(first_name, 3) from actor;
select first_name, substr(first_name, 2, 4) from actor;
-- trim => used to cut the given symbol from the string
select length(trim(" hello ")) from dual;
select length(trim(leading "h" from " hello")) from dual;
select length(trim(trailing "o" from "helloooo ")) from dual;
select length(trim(both "x" from " xx hey xxxxxx")) from dual;
-- ltrim()
select length(ltrim(" hello ")) from dual;
-- rtrim()
select length(rtrim(" hello ")) from dual;
-- WANT TO GET DATA BASED ON CERTAIN FORMAT, SIZE OR LENGTH
-- lpad/rpad keep adding characters in starting/ending
-- majorly used for numerical data
-- lpad()
select first_name, lpad(first_name, 4, "_") from actor;
-- rpad()
select first_name, rpad(first_name, 4, "_") from actor;
-- instr() => find the occurrence of an alphabet
select first_name, instr(first_name, "e") from actor;
-- locate() => search the occurrence of an aphabet from a certain position
select first_name, locate("e", first_name, 3) from actor;
-- replace() => replace a substring with another string
select first_name, replace(first_name, "E", "#") from actor;
-- strcmp() => compares two strings, whether equal or not
select strcmp("nick", "nick") from dual; -- 0
select strcmp("nick", "nicks") from dual; -- -1
select strcmp("nicks", "nick") from dual; -- 1
select strcmp("nicks", null) from dual; -- null
-- MATH FUNCTIONS
-- mod()
select mod(10,3) from dual; -- 1
select 10%3 from dual; -- 1
-- pow()
select pow(2,4) from dual; -- 16
-- sqrt()
select sqrt(16) from dual; -- 4
-- rand()
select rand() from dual; -- returns random floating value
-- round()
select round(10.247); -- 10
select round(10.547); -- 11
select round(10.247, 2); -- 10.25
select round(10.242, 2); -- 10.24
select round(83.97, -1); -- 80
select round(87.97, -1); -- 90
select round(263.80, -2); -- 300
select round(213.80, -2); -- 200
select truncate(10.247, 2); -- 10.24
select truncate(246.247, -2); -- 200
select truncate(876.247, -2); -- 800
select floor(24.16); -- 24
select ceil(24.16); -- 25