all groups > sql server programming > june 2007 >
You're in the

sql server programming

group:

Converting a String to a table of characters


Converting a String to a table of characters stjulian
6/25/2007 6:35:26 PM
sql server programming:
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

Re: Converting a String to a table of characters Mike C#
6/25/2007 7:12:16 PM
Your database collation is case-insensitive. Change the collation to
case-sensitive.

[quoted text, click to view]

Re: Converting a String to a table of characters stjulian
6/25/2007 7:25:49 PM
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



[quoted text, click to view]

Re: Converting a String to a table of characters stjulian
6/25/2007 7:33:17 PM
I am going to turn this into a stored procedure. Will the @tbl be removed
out of memory along with the rest of the variables after the SP executes and
returns the table? Is there anything that has to be done with variables and
tables creates in a stored procedure or do they disappear on their own?

Julian


[quoted text, click to view]

Re: Converting a String to a table of characters Mike C#
6/25/2007 8:38:06 PM

[quoted text, click to view]

Sure, that'll do. Variables and table variables have well-defined scope, so
they exist only during execution of the SP.

Re: Converting a String to a table of characters Tom Cooper
6/25/2007 11:04:54 PM
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]

AddThis Social Bookmark Button