How to Loop From A to Z in T-SQL
Microsoft SQL Server uses Transact-SQL (T-SQL) as its primary query language. T-SQL "WHILE" loops allow for repeating a process while incrementing a value or looking for a specific condition before exiting the loop. Looping through numeric values is a simple process; however, an alphabetic loop requires translating letters into their equivalent ASCII character codes and then using the "CHAR()" function to convert them back to letters. The ASCII codes for the uppercase letters of the alphabet are values 65 through 90.
The lowercase alphabet is represented by values 97 through 122.
Instructions
1 Open SQL Server Management Studio.
2 Open a new query window.
3 Declare an integer variable and set its value to 65 as shown:
DECLARE @intCharCode INT
SET @intCharCode = 65
4 Type the following "WHILE" statement to output the ASCII character equivalent of the integer code represented by the variable. The loop increments the variable by a value of 1 with each iteration until it reaches 90:
WHILE NOT (@intCharCode > 90)
BEGIN
PRINT CHAR(@intCharCode)
SET @intCharCode = @intCharCode + 1
END
5 Click the execute button to run the query. The output will print the uppercase alphabet in the results pane in alphabetic order.
Microsoft SQL Server uses Transact-SQL (T-SQL) as its primary query language. T-SQL "WHILE" loops allow for repeating a process while incrementing a value or looking for a specific condition before exiting the loop. Looping through numeric values is a simple process; however, an alphabetic loop requires translating letters into their equivalent ASCII character codes and then using the "CHAR()" function to convert them back to letters. The ASCII codes for the uppercase letters of the alphabet are values 65 through 90.
The lowercase alphabet is represented by values 97 through 122.
Instructions
1 Open SQL Server Management Studio.
2 Open a new query window.
3 Declare an integer variable and set its value to 65 as shown:
DECLARE @intCharCode INT
SET @intCharCode = 65
4 Type the following "WHILE" statement to output the ASCII character equivalent of the integer code represented by the variable. The loop increments the variable by a value of 1 with each iteration until it reaches 90:
WHILE NOT (@intCharCode > 90)
BEGIN
PRINT CHAR(@intCharCode)
SET @intCharCode = @intCharCode + 1
END
5 Click the execute button to run the query. The output will print the uppercase alphabet in the results pane in alphabetic order.
Thanks a lot.
ReplyDeleteStainless Steel Double Edge Razor - Tiagear.com
ReplyDeleteStainless steel 2014 ford fusion energi titanium double edge razor. ridge titanium wallet Features the titanium trim most advanced engineering tools babylisspro nano titanium hair dryer available. These blades provide an exceptional performance in wet shaving $54.00 · In is titanium a metal stock