{"id":224,"date":"2011-12-20T16:43:25","date_gmt":"2011-12-20T23:43:25","guid":{"rendered":"http:\/\/www.sqldbpros.com\/?p=224"},"modified":"2011-12-20T16:56:26","modified_gmt":"2011-12-20T23:56:26","slug":"sql-alphabet-table-get-next-character","status":"publish","type":"post","link":"http:\/\/sqldbpros.com\/wordpress\/2011\/12\/sql-alphabet-table-get-next-character\/","title":{"rendered":"SQL Alphabet Table: Get Next Character"},"content":{"rendered":"<p>While writing some dynamic SQL to cycle through a set of Active Directory LDAP queries (don't ask, ugh) I needed to dynamically create a table with the values 0-9 and A-Z in it.\u00a0 After a few minutes,<!--more--> okay maybe 30, over work I came up with the following T-SQL script.\u00a0 It's been tested on MS SQL Server 2008 R2.\u00a0 It should work on SQL Server Server 2000 and 2005 as well with minor mods.\u00a0 Give it a shot and let me know.<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>--create a table to store the values DECLARE @Characters TABLE ( ID INT IDENTITY(1,1), Characters NCHAR(1) PRIMARY KEY )<\/p>\n<p>--insert numbers 0-9 DECLARE @num INT= 0 WHILE @num &lt;= 9 BEGIN<\/p>\n<p>INSERT\u00a0 @Characters ( Characters ) SELECT\u00a0 @num<\/p>\n<p>SELECT\u00a0 @num = @num + 1<\/p>\n<p>END<\/p>\n<p>--add letters A-Z\u00a0 (aka ASCII 65-90) DECLARE @asciiCode INT= 65 WHILE @asciiCode &lt;= 90 BEGIN<\/p>\n<p>INSERT\u00a0 @Characters ( Characters ) SELECT\u00a0 CHAR(@asciiCode)<\/p>\n<p>SELECT\u00a0 @asciiCode = @asciiCode + 1<\/p>\n<p>END<\/p>\n<p>--Be awesome!<\/p>\n<p>SELECT * FROM @Characters AS c<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>While writing some dynamic SQL to cycle through a set of Active Directory LDAP queries (don't ask, ugh) I needed to dynamically create a table with the values 0-9 and A-Z in it.\u00a0 After a few minutes,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,3],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-microsoft-sql-server","category-scripts"],"_links":{"self":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":7,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":232,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/224\/revisions\/232"}],"wp:attachment":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}