-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_StringFunciton.sql
71 lines (50 loc) · 1.74 KB
/
SQL_StringFunciton.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
/*
String Functions - TRIM, LTRIM, RTRIM, Replace, Substring, Upper, Lower
*/
-- Drop Table EmployeeErrors
CREATE TABLE EmployeeErrors(
EmployeeID varchar(50),
EmployeeName varchar(50)
)
Insert Into EmployeeErrors values
('1001','Jimbo Halbert'),
('1002','Pamela Beasely'),
('1005', 'Toby Flendrson-Fired')
update EmployeeErrors
set EmployeeID= ' 1002 '
where EmployeeName = 'Pamela Beasely';
Select * from EmployeeErrors
--Using Trim, Left-Trim, Right-Trim
Select EmployeeID, Trim(EmployeeID) As TrimID
from EmployeeErrors
Select EmployeeID, LTrim(EmployeeID) As LTrimID
from EmployeeErrors
Select EmployeeID, RTrim(EmployeeID) As RTrimID
from EmployeeErrors
--Using Replace
Select EmployeeName, REPLACE(EmployeeName, '-Fired','')
from EmployeeErrors
-- Using Substring
-- Buzzy match
Select SUBSTRING(EmployeeName,3,3)
from EmployeeErrors
Select Err.EmployeeName,substring(Err.EmployeeName,2,3),Emp.EmployeeName, substring(Emp.EmployeeName,2,3)
from EmployeeErrors As Err
Join [SQL Tutorial].dbo.Employee As Emp
on substring(Err.EmployeeName,2,3)= substring(Emp.EmployeeName,2,3)
-- Using UPPER and LOWER
Select EmployeeName, upper(EmployeeName) AS UpperName
from EmployeeErrors
Select EmployeeName, LOWER(EmployeeName) As LowerName
from EmployeeErrors
-- CONCAT() returns concatenated string
SELECT CONCAT('abc','def');
SELECT CONCAT('abc',' ','def');
-- LEFT() returns the leftmost number of characters as specified
SELECT LEFT('asdfhgl',4)
-- RIGHT() returns the rightmost number of characters as specified
SELECT Right('asdfhgl',4)
-- Length() returns the length of a string
Select LEN('adfghgf')
-- Reverse() reverse the order of characters in a string
Select REVERSE('apple')