What you have will work. The disadvantage is that in SQL set based
solutions are almost always faster than loops or cursors. And with the
right tables, this can be done with a simple Select statement.
First, create your tblAsciiCodes table with the unicode_character column
having a collation of SQL_Latin1_General_CP1_CS_AS (or some other case
sensitive collation). That will tell SQL that for this column you want
upper case letters (eg, 'A') to be considered different from lower case
letters (eg, 'a') when doing comparisions on this column (See COLLATE in BOL
for a discussion of collations in SQL). The table should look something
like:
Create Table tblAsciiCodes (
unicode_character nchar(1)
Collate SQL_Latin1_General_CP1_CS_AS Primary Key,
letterdescription varchar(20))
and load it with the appropriate values.
Then create a Numbers table. See
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html
for how to do this and some of the ways you can use one.
Once you have it, you can get the result you want with the query
Select n.Number As Position,
Unicode(a.unicode_character) As unicode_code,
a.unicode_character,
a.letterdescription
From Numbers n
Inner Join tblAsciiCodes a On Substring(@MyString, n.Number, 1) =
a.unicode_character
Where n.Number <= Len(@MyString)
Order By n.Number
Tom
[quoted text, click to view] "stjulian" <stjulian@discussions.microsoft.com> wrote in message
news:eDQEsA4tHHA.768@TK2MSFTNGP04.phx.gbl...
> Well, I came up with this
>
> DECLARE @pos INT
> DECLARE @Mystring Varchar(50)
> DECLARE @CharCode INT
> DECLARE @tbl TABLE (position INT, code INT)
>
> SET @pos = 1
> SET @MyString='HaPPiness'
>
> WHILE @pos <= LEN(@MyString)
> BEGIN
> SET @CharCode = UNICODE(SUBSTRING(@Mystring, @pos, 1))
> INSERT @tbl(position, code) VALUES (@pos, @CharCode)
> SET @pos = @pos + 1
> END
>
> SELECT position, B.unicode_code, B.unicode_character, B.letterdescription
> FROM @tbl A
> INNER JOIN tblAsciiCodes B on A.code = B.unicode_code
> ORDER BY position
>
>
> the result:
>
>
> position unicode_code unicode_character letterdescription
> ----------- ------------ ----------------- --------------------
> 1 72 H Uppercase Hotel
> 2 97 a Lowercase Alpha
> 3 80 P Uppercase Papa
> 4 80 P Uppercase Papa
> 5 105 i Lowercase India
> 6 110 n Lowercase November
> 7 101 e Lowercase Echo
> 8 115 s Lowercase Sierra
> 9 115 s Lowercase Sierra
>
> (9 row(s) affected)
>
>
>
> How did I do?
>
> Julian
>
>
>
> "Mike C#" <xyz@xyz.com> wrote in message
> news:euVbI53tHHA.4612@TK2MSFTNGP04.phx.gbl...
>> Your database collation is case-insensitive. Change the collation to
>> case-sensitive.
>>
>> "stjulian" <stjulian@discussions.microsoft.com> wrote in message
>> news:OEdMhk3tHHA.4972@TK2MSFTNGP05.phx.gbl...
>>> SQL2000
>>>
>>> I have created a table of characters, ascii codes and the military call
>>> words (alpha, bravo, etc.) and would like to create a SELECT statement
>>> to convert a string into a table to match up to the letters in the
>>> table.
>>>
>>> my ascii code table:
>>>
>>> unicode_code unicode_character description
>>> 65 A Uppercase Alpha
>>> 66 B Uppercase Bravo
>>> ...
>>>
>>>
>>> I would like to take a string
>>> @MyString = "Apple"
>>>
>>> to return a table showing each letter in the string from the above
>>> table, and (hopefully) a new column showing the character position, in
>>> this case 1-5 for the 5 characters in @MyString.
>>>
>>> The intersting thing is that SQL seems to match uppercase "A" with
>>> lowercase "a"
>>>
>>> Any ideas?
>>>
>>>
>>> Thank You,
>>>
>>> Julian
>>>
>>
>>
>
>