belle’s sql musings

List all ASCII characters

Posted in DBA Toolbox / T-SQL Scripts, T-SQL Tips and Tricks by belle on June 9, 2008
   1: -- ---------------------------------------------------------------------
   2: -- displays ASCII characters, and identify which ones are printable
   3: -- ---------------------------------------------------------------------
   4: DECLARE @min int, @max int
   5: DECLARE @chars TABLE
   6: ( 
   7:     ASCIIValue       int,
   8:     ASCIIString      nvarchar(3),
   9:     IsPrintable      bit 
  10: )
  11: SET @min = 0
  12: SET @max = 300
  13:  
  14: -- Codes 33 to 126 are printable characters
  15: -- http://en.wikipedia.org/wiki/ASCII
  16: WHILE @min <= @max
  17: BEGIN
  18:     INSERT INTO @chars
  19:     SELECT 
  20:         @min, 
  21:         CHAR(@min),
  22:         CASE 
  23:             WHEN @min BETWEEN 33 AND 126 THEN 1
  24:             ELSE 0
  25:         END
  26:     SET @min = @min+1
  27: END
  28:  
  29: SELECT 
  30:     * 
  31: FROM 
  32:     @chars
  33: WHERE 
  34:     ASCIIString IS NOT NULL
  35:  
  36:  
  37:  

Advertisements
Tagged with:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: