-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Manipulation.sql
More file actions
140 lines (107 loc) · 5.95 KB
/
String_Manipulation.sql
File metadata and controls
140 lines (107 loc) · 5.95 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-- select where jobtitle is engineering manager and tool designer
select BusinessEntityID, JobTitle, BirthDate
from [HumanResources].[Employee]
where JobTitle in ('Engineering manager', 'Tool Designer')
-- select BusinessEntityID, BirthDate, JobTitle WHERE BusinessEntityID between 100 AND 200
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where BusinessEntityID between 100 AND 200
-- select BusinessEntityID, BirthDate, JobTitle between range of date of birth
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where BirthDate BETWEEN '1980-01-01' AND '2010-12-31'
-- get BusinessEntityID, BirthDate, JobTitle with job title starting between c and g
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle BETWEEN 'c' AND 'g'
-- another may to do this
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like '[c-g]%'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who jobtitle starts with E
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like 'e%'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who has at least one 'e' anywhere in their jobtitle
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like '%e%'
-- get me the BusinessEntityID, BirthDate, JobTitle of all employees whos jobtitle's 3th character from the begining is 'r'
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like '__r%'
-- get me the BusinessEntityID, BirthDate, JobTitle of all employees whos jobtitle's 4th character from the begining is 'k' and 5th character form last is 'a'
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like '___k%' and JobTitle like '%a____'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who has no 'e' in their jobtitle
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle not like '%e%'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who has at most three 'a' in their jobtitle
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle not like '%a%a%a%a%'
--get get the employee who lastname do not start with a to g and end with s
select LastName
from [Person].[Person]
where lastname like '[^a-g]%s'
-- get the employee who lastname do not start with a to g and the second character from the begining must be either 'a or 'b and should end with s
select LastName
from [Person].[Person]
where lastname like '[^a-g][ab]%s'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who has at least one '%' anywhere in their jobtitle
select BusinessEntityID, BirthDate, JobTitle
from [HumanResources].[Employee]
where JobTitle like '%b%%' escape 'b'
--get me all employees who has at least one '_' anywhere in their jobtitle
select LastName
from [Person].[Person]
where lastname like '%b_%' escape 'b'
--get me BusinessEntityID, BirthDate, JobTitle of all employees who has at least 2 '%' anywhere in their jobtitle and end with underscore
select LastName
from [Person].[Person]
where lastname like '%+%%+%%+_' escape '+'
-- another way to do this
select LastName
from [Person].[Person]
where lastname like '%[%]%[%]%[_]'
-- get the employee who has at leat ' anythere in their last name, apostriphe is wildcard for the server
select LastName
from [Person].[Person]
where lastname like '%''%'
-- get the employee who has at leat two apostrophe anythere in their last name, apostriphe is wildcard for the server
select LastName
from [Person].[Person]
where lastname like '%''%''%'
-- get all the emplyees whos lastname is enclosed in apostrophe like 'lane'.
select LastName
from [Person].[Person]
where lastname like '''%'''
-- get the fullname using
select BusinessEntityID, FirstName, MiddleName, lastname, concat(firstname, ' ', MiddleName, ' ' , lastname) as fullname
from [Person].[Person]
-- get the fullname using but + sign prevent extrat space if the middlename is null,
select BusinessEntityID, FirstName, MiddleName, lastname, concat(firstname, ' '+ MiddleName, ' ' , lastname) as fullname
from [Person].[Person]
--get me the processing time of each orders to process in the facility and arrange it from worst to best
select [SalesOrderID] , [OrderDate] ,[ShipDate] , DATEDIFF(day, [OrderDate], [ShipDate]) as responsetime
from AdventureWorks2016.[Sales].[SalesOrderHeader]
order by responsetime desc -- everything come after order by is called sorting predicates
-- get this formating : monday 18, 2022 (Apr 18 22)
select [SalesOrderID], [OrderDate] ,[ShipDate] , DATEDIFF(day, [OrderDate], [ShipDate]) as responsetime,
datename(dw, [OrderDate]) + ' ' + cast(day([OrderDate]) as varchar(2)) + ',' +
cast(year([OrderDate]) as varchar(4)) + ' (' + left (datename(month,[OrderDate]), 3) + ' '+ cast(day([OrderDate]) as varchar(2))+
' ' + right(cast(year([OrderDate]) as varchar(4)), 2)+ ')' orderdatenew
from AdventureWorks2016.[Sales].[SalesOrderHeader]
order by responsetime desc
-- get me initial of all the person who has a middlename and arrange the dataset in the sorted order of the 3rd of their firstname */
select BusinessEntityID, FirstName, LastName, concat(left(FirstName, 1), '.', LEFT([MiddleName], 1) , '.', left(LastName, 1)) initial
from AdventureWorks2016.person.Person
where [MiddleName] is not null
order by substring(firstname, 3,1)
-- ckeck if leap year or not
select iif(day(EOMONTH ([OrderDate])) = 28 ,'not a leap year', 'a leap year') as year
from AdventureWorks2016.[Sales].[SalesOrderHeader]
-- clean whitespace inside string
select CONCAT(left('san Diego', charindex(' ','san Diego', 1)-1) , ' ' , reverse(left(reverse('san Diego'), charindex(' ', reverse('san Diego'),1) -1)))