Facebook

Saturday, 28 July 2012

How to Loop From A to Z in T-SQL

             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

    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

  Click the execute button to run the query. The output will print the uppercase alphabet in the results pane in alphabetic order.



2 comments:

  1. Stainless Steel Double Edge Razor - Tiagear.com
    Stainless 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

    ReplyDelete