-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22-BuiltInFunction.sql
More file actions
242 lines (195 loc) · 5.86 KB
/
22-BuiltInFunction.sql
File metadata and controls
242 lines (195 loc) · 5.86 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
/*
String Functions
Built in string functions in sql server 2008 - Part 22
Functions in SQL server can be broadly divided into 2 categoris
1. Built-in functions
2. User Defined functions
There are several built-in functions. In this video session, we will look at the most common string functions available.
ASCII(Character_Expression) - Returns the ASCII code of the given character expression.
To find the ACII Code of capital letter 'A'
Example: Select ASCII('A')
Output: 65
CHAR(Integer_Expression) - Converts an int ASCII code to a character. The Integer_Expression, should be between 0 and 255.
The following SQL, prints all the characters for the ASCII values from o thru 255
Declare @Number int
Set @Number = 1
While(@Number <= 255)
Begin
Print CHAR(@Number)
Set @Number = @Number + 1
End
Note: The while loop will become an infinite loop, if you forget to include the following line.
Set @Number = @Number + 1
Printing uppercase alphabets using CHAR() function:
Declare @Number int
Set @Number = 65
While(@Number <= 90)
Begin
Print CHAR(@Number)
Set @Number = @Number + 1
End
Printing lowercase alphabets using CHAR() function:
Declare @Number int
Set @Number = 97
While(@Number <= 122)
Begin
Print CHAR(@Number)
Set @Number = @Number + 1
End
Another way of printing lower case alphabets using CHAR() and LOWER() functions.
Declare @Number int
Set @Number = 65
While(@Number <= 90)
Begin
Print LOWER(CHAR(@Number))
Set @Number = @Number + 1
End
LTRIM(Character_Expression) - Removes blanks on the left handside of the given character expression.
Example: Removing the 3 white spaces on the left hand side of the ' Hello' string using LTRIM() function.
Select LTRIM(' Hello')
Output: Hello
RTRIM(Character_Expression) - Removes blanks on the right hand side of the given character expression.
Example: Removing the 3 white spaces on the left hand side of the 'Hello ' string using RTRIM() function.
Select RTRIM('Hello ')
Output: Hello
Example: To remove white spaces on either sides of the given character expression, use LTRIM() and RTRIM() as shown below.
Select LTRIM(RTRIM(' Hello '))
Output: Hello
LOWER(Character_Expression) - Converts all the characters in the given Character_Expression, to lowercase letters.
Example: Select LOWER('CONVERT This String Into Lower Case')
Output: convert this string into lower case
UPPER(Character_Expression) - Converts all the characters in the given Character_Expression, to uppercase letters.
Example: Select UPPER('CONVERT This String Into upper Case')
Output: CONVERT THIS STRING INTO UPPER CASE
REVERSE('Any_String_Expression') - Reverses all the characters in the given string expression.
Example: Select REVERSE('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
Output: ZYXWVUTSRQPONMLKJIHGFEDCBA
LEN(String_Expression) - Returns the count of total characters, in the given string expression, excluding the blanks at the end of the expression.
Example: Select LEN('SQL Functions ')
Output: 13*/
--
Declare @Number int
Set @Number = ASCII('A')
While(@Number <= ASCII('A')+25)
Begin
print CHAR(@Number)
Set @Number = @Number + 1
End
/*
Note: The while loop will become an infinite loop, if you forget to include the following line.
Set @Number = @Number + 1
*/
--Print Ascii value
Select ASCII('A') AS CHARACTER
--using loops
Declare @i int
set @i = 0
while @i < 26
begin
print char(@i+65)
set @i = @i+1
end
/*
Print All character
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
Z
*/
--Use of 'Ltrim'
--use print
Print('My name is Firoz');
--Print statement to print a variable value
Declare @Msg varchar(300) = 'My name is Firoz';
PRINT @msg;
--SQL server PRINT statement to print an integer value
Declare @a int = 1000;
PRINT @a
-- Assigning a value to a variable with a Scalar Subquery using SET
use My_new_database
Go
select * from tblEmployee;
Declare @start int ,@city_name varchar(10)
set @start = 1
while @start <= 10
begin
set @city_name = (Select city from tblEmployee where ID = @start)
Print @city_name;
set @start = @start + 1
end
--interger variable and a string in the PRINT statement
Declare @a INT = 1000
Print ('Your queue no is ' + @a )
--Conversion failed when converting the varchar value 'Your queue no is ' to data type int.
--interger variable and a string in the PRINT statement
--With cast function
Declare @a INT = 1000
Print ('Your queue no is ' + cast(@a AS VARCHAR(10)))
--use of sql CONCATANATION
Declare @a INT = 1000
Print CONCAT('Your queue no is ', @a )
--Your queue no is 1000
Declare @a XML = '<CustomerID="1" CustomerName="Rajendra"/>'
print @a
--Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query.
--Cast or convert
Declare @a XML = '<CustomerID="1"CustomerName="Rajendra"/>'
print cast(@a as varchar(100))
Print 'My name is Firoz Mahmud';
WAITFOR DELAY '00:00:3';
Print 'You are reading articale on SQL PRINT statement';
WAITFOR DELAY '00:00:3';
Declare @a INT
SET @a = 1;
while(@a < 15)
begin
PRINT CONCAT('This is Iteration no ' , @a)
waitFor DELAY'00:00:01'
SET @a = @a + 1
END
--Use Of RAISERROR
raiserror('My name is Firoz Mahmud', 1, 1) with nowait;
waitfor delay '00:00:03';
raiserror('You are reding artical on sql print statement', 16, 1) with nowait;
waitfor delay '00:00:03';
DECLARE @a INT;
SET @a = 1;
WHILE(@a < 15)
BEGIN
RAISERROR('This is Iteration no:', @A, 0, 1) WITH NOWAIT;
SET @a = @a + 1;
END;
DECLARE @a INT
SET @a = 1;
DECLARE @S VARCHAR(100)= 'This is iteration no';
WHILE(@a < 5)
BEGIN
RAISERROR('%s:%d', 0, 1, @s, @a) WITH NOWAIT;
SET @a = @a + 1;
END;
/****************************/
select LTRIM(' Hello');
select (' Hello ');
select Rtrim('hello ');