-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23-Function.sql
More file actions
325 lines (283 loc) · 6.47 KB
/
23-Function.sql
File metadata and controls
325 lines (283 loc) · 6.47 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Ascii
*/
--Use of ASCII function
use My_new_database
GO
select * from tblEmployee
Select ASCII(Name) AS NumCodeOfFirstChar From tblEmployee;
--Use of CHAR() FUNCTION
Select CHAR(65) AS CodeToCharacter
--Use Of CHARINDEX() Function
Select CHARINDEX('t','Coustomer') AS MatchPosition
--return 5
Declare @position int, @substr nvarchar(10) = 'to';
set @position = CHARINDEX(@substr,'Coustomer')
print(@substr + ' ' + cast(@position as varchar (10)))
set @position = CHARINDEX(@substr,'Coustomer', 4)
print(@substr + ' ' + cast(@position as varchar (10)))
--Use CONCAT
Select CONCAT('Hello ', 'there') AS WELLCOME
select concat('SQL', ' is', ' fun!') as PRINTF
--Concat with +
select 'Hello' + ' Wolrd';
select 'SQL' +' is' + ' fun!' As PRINTF
--use of CONCAT_WS()
select CONCAT_WS('.','www','Supersonic', 'com');
--//www.Supersonic.com
select CONCAT_WS('-','SQL',' IS',' FUN!') As PRINTF
--//SQL- IS- FUN!
--Use of DATALENGTH() function
Select DATALENGTH('www.facebook.com')
--//16
--Difference() Function
select difference('Juice', 'Jucy');
--//4
select difference('Juice', 'Banana');
--//2
--Format() function
Declare @d datetime = '12/01/2018';
select
FORMAT(@d, 'd', 'en-US') AS 'US English Result',
FORMAT(@d, 'd', 'no') as 'Norwegin Result',
FORMAT(@d, 'd', 'zu') as 'ZULU result';
--//12/1/2018 01.12.2018 12/1/2018
Select FORMAT(123456789, '##-##-####');
--//123-45-6789
--SQL server LEFT() function
select left('SQL Tutorial', 3) as ExtractString
--//SQL
select left('SQL Tutorial', 6) as ExtractString
--//SQL Tu
select * from tblEmployee
Select Left(Name, 5) as ExtractStrign,left(city , 3) AS CITY From tblEmployee
/*
Tom Lon
Pam New
John Lon
Sam Lon
Todd Syd
Ben New
Sara Syd
Valar New
James Lon
Russe Lon
Firoz Dha
*/
Select LEFT('SQL Tutorial', 100) as ExtractString
--//SQL Tutorial
--SQL server LEN() Function
Select LEN('www.facebook.com');
--//16
--SQL server LEN() Function
Select LEN(' www.facebook.com');
--//19
--SQL server LEN() Function
Select LEN('www.facebook.com ');
--//16
--SQL Server LOWER() FUNCTION
select LOWER('SQL Turorial is FUN !');
--//sql turorial is fun !
select * from tblEmployee
Select id, Lower(Name) as NAME,lower(gender) as GENDER, Salary, lower(city) as CITY from tblEmployee
/*
1 tom male 4000 london
2 pam female 3000 new york
3 john male 3500 london
4 sam male 4500 london
5 todd male 2800 sydney
6 ben male 7000 new york
7 sara female 4800 sydney
8 valarie female 5500 new york
9 james male 6500 london
10 russel male 8800 london
11 firoz male 0 dhaka
*/
--Upper()
Select id, Upper(Name) as NAME,Upper(gender) as GENDER, Salary, Upper(city) as CITY from tblEmployee
/*
1 TOM MALE 4000 LONDON
2 PAM FEMALE 3000 NEW YORK
3 JOHN MALE 3500 LONDON
4 SAM MALE 4500 LONDON
5 TODD MALE 2800 SYDNEY
6 BEN MALE 7000 NEW YORK
7 SARA FEMALE 4800 SYDNEY
8 VALARIE FEMALE 5500 NEW YORK
9 JAMES MALE 6500 LONDON
10 RUSSEL MALE 8800 LONDON
11 FIROZ MALE 0 DHAKA
*/
--SQL server LTRIM() function
--Remove leadin space from a string
select ' Firoz Mahmud' as Name
-- Firoz Mahmud
Select LTRIM(' Firoz Mahmud') As LeftTrimmedString
--Firoz Mahmud
--SQL server RTRIM() function
--Remove leadin space from a string
select ' Firoz Mahmud ' as Name
-- Firoz Mahmud
Select RTRIM(' Firoz Mahmud ') As LeftTrimmedString
-- Firoz Mahmud
--NCHAR() Function
Select NCHAR(65) as NumberCodeToUnicode
--//A
Declare @start int;
set @start = 0;
while(@start < 25)
BEGIN
print NCHAR(65+@start)
set @start = @start + 1
END
/*
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
*/
--PATINDEX() function
--Return the position of a patteran on a string
select PATINDEX('%school%' , 'W3School.com');
--//3
select PATINDEX('%s%com%' , 'W3School.com');
--//3
select PATINDEX('%[ol]%' , 'W3School.com');
--//6
select PATINDEX('%[z]%' , 'W3School.com');
--//6
--QUOTENAME() function
SELECT QUOTENAME('abcdef');
--[abcdef]
SELECT QUOTENAME('abcdef', '()');
--(abcdef)
--Replace() function
select REPLACE('SQL Tutorial', 'T', 'M');
--//SQL MuMorial
select REPLACE('SQL Tutorial', 'SQL', 'HTML');
--//HTML Tutorial
--REPLICATE() function
select REPLICATE('SQL Tutorial', 5);
--SQL TutorialSQL TutorialSQL TutorialSQL TutorialSQL Tutorial
select REPLICATE(Name, 2) from tblEmployee
/*
TomTom
PamPam
JohnJohn
SamSam
ToddTodd
BenBen
SaraSara
ValarieValarie
JamesJames
RusselRussel
FirozFiroz
*/
--REVERSE() function
select REVERSE(Name) from tblEmployee
/*
moT
maP
nhoJ
maS
ddoT
neB
araS
eiralaV
semaJ
lessuR
zoriF
*/
--SOUNDEX() function
--Evaluate the similarity of two string, and return a four character code
select SOUNDEX('Juice'), SOUNDEX('Jucy');
--//J200 J200
select SOUNDEX('Juice'), SOUNDEX('Banana');
--//J200 B550
--SPACE()
--Return a string with 10 space
select space(10);
print ('Firoz' + space(10) + 'Mahmud');
--Firoz Mahmud
--STR() function
--return a number of string
select str(185.476, 6, 3);
--//185.48
--STUFF() function
--delete 3 character from a string, string in position 1, and than insert 'HTML' in position 1:
select STUFF('SQL Tutorial', 1, 3, 'HTML');
--//HTML Tutorial
select STUFF('SQL Tutorial', 1, 13, 'is fun');
--is fun
select STUFF('SQL Tutorial', 13, 1, 'is fun');
//NULL
--SUBSTRING() function
SELECT SUBSTRING('SQL Tutorial', 1, 3);
--//SQL
select SUBSTRING(Name,1,3) as NAME from tblEmployee
/*
Tom
Pam
Joh
Sam
Tod
Ben
Sar
Val
Jam
Rus
Fir
*/
--TRANSLATE() function
--Return the string from the first argument AFTER the character specified in the second are
--translated into the character specified in the third argument
select TRANSLATE('Monday', 'Monday', 'Sunday');
--//Sunday
SELECT TRANSLATE('3*[2+1]/{8-4}', '[]{}', '()()'); -- Results in 3*(2+1)/(8-4)
--Trim() function
--Removing leading and trailing space a string
select TRIM(' SQL Tutorial! ') as TrimmedString;
--//SQL Tutorial!
SELECT TRIM('#! ' FROM ' #SQL Tutorial! ') AS TrimmedString;
SELECT TRIM('SL ' FROM ' SQL Tutorial ') AS TrimmedString;
--//QL Tutoria
--Unicode() function
select UNICODE('Atlanta');
--65
select UNICODE('Firoz');
--70
select UNICODE(Name) as Name , Name From tblEmployee
/*
84 Tom
80 Pam
74 John
83 Sam
84 Todd
66 Ben
83 Sara
86 Valarie
74 James
82 Russel
70 Firoz
*/