SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert ASCII strings to the number value groups?


Convert ASCII strings to the number value groups?

Author
Message
SQL Dummy-659258
SQL Dummy-659258
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 12
Please help me, my ignorance of SQL and ASCII has lead me to lose temendous amount of time trying to do this task.

I have ascii characters in an existing table column that range from single characters to sequentially doubled and tripled characters in range from 0 - 255. My goal is to read each string from each record and convert each charter in that string to it's number equivalent, group the numbers for the string and update another column in the same table with the number. ASCII strings (shown here separated by commas) like. '"/, aA╢, a8σ, %┐,{ '}{

I found this in SQL Server (ASCII convertion) help topic: ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/45c2044a-0593-4805-8bae-0fad4bde2e6b.htm
But I'm SQL stupid and don't know how to loop, convert, group the values then pass the result back to the table in integer form.

Below is what I've been trying to modify to accomplish this task and not getting anywhere.

SET TEXTSIZE 0

-- Create variables for the character string and for the current
-- position in the string.

DECLARE @position int, @string char(8), @myval int

-- Initialize the current position and the string variables.

SET @position = 1

--here I'm trying to load the @string with a select statement ASCII string column
SET @string = SELECT dbo.FCCFRE.LIC_ID
FROM dbo.FCCFRE

--I want to update this column with the numeric value after it resolves the ASCII string to an integer string.
--I know this code puts out each code separately, but I need to group the values.
FOR UPDATE OF FRE_ID
--need to loop through each record but not know how to do it

WHILE @position <= DATALENGTH(@string)
BEGIN
SELECT ASCII(SUBSTRING(@string, @position, 1)),
CHAR(ASCII(SUBSTRING(@string, @position, 1)))
SET @position = @position + 1
END
GO
--Don't know how to pass the number to @Myval and update the FRE_ID column.

This forum is my last resort... Please let me know if this is too difficult for this forum.
TIA
WayneS
WayneS
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22368 Visits: 10657
Does this help you out?
declare @str varchar(8000);
set @str = 'qothobnslfdnoweavnivmnqpwf';

;WITH
TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),
MILLIONS (N) AS (SELECT 1 FROM THOUSANDS t1 CROSS JOIN THOUSANDS t2),
TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM MILLIONS)
SELECT N,
LetterAsPosition = substring(@str, N, 1),
AsciiAtPosition = ascii(substring(@str, N, 1))
FROM TALLY
WHERE N <= datalength(@str)
ORDER BY N;



Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

SQL Dummy-659258
SQL Dummy-659258
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 12
Thanks very much for your post.
This a good start but, I put in "!┐" for the string and the ascii function thinks that the string
is"!*". the ascii function stops at 127.

Is there a way to do this for the entire range of 0-255?
Thanks for sharing your knowledge!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222472 Visits: 42003
SQL Dummy-659258 (10/27/2010)
the ascii function stops at 127.


Actually, it doesn't. You just can't pass the ASCII function more than one character at a time.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222472 Visits: 42003
Here's the hard part... what do you want to display for the value aA╢

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222472 Visits: 42003
Never mind... can't wait for an answer. Here's 95% of the solution. All you have to do is figure out how you actually want to format the 0-255 numbers in the final column. Do note that the ASCII function works just fine for characters over 127. You just have to be careful to only pass it 1 character at a time.

--=============================================================================
-- Create some test data. This is NOT a part of the solution
--=============================================================================
--===== Conditionally drop the test table to make reruns easier
IF OBJECT_ID('tempdb..#MyHead','U') IS NOT NULL
DROP TABLE #MyHead
;
--===== Build and populate the test table on the fly
WITH
cteBuild3CharData AS
( --=== Builds 3 random characters and a random 1-3 length
SELECT TOP 100000
WierdData = CAST(CHAR(ABS(CHECKSUM(NEWID()))%256) AS VARCHAR(3))
+ CAST(CHAR(ABS(CHECKSUM(NEWID()))%256) AS VARCHAR(3))
+ CAST(CHAR(ABS(CHECKSUM(NEWID()))%256) AS VARCHAR(3)),
RandomLen = ABS(CHECKSUM(NEWID()))%3+1
FROM sys.all_columns ac1,
sys.all_columns ac2
) --=== Use the random length to determine how long a string to save
-- and put it all in the test table
SELECT WierdData = SUBSTRING(WierdData,1,RandomLen)
INTO #MyHead
FROM cteBuild3CharData
;
--=============================================================================
-- In the absence of clear requirements, solve most of the problem.
--=============================================================================
--===== Display the Original data and 0-255 for each available character
SELECT WierdData,
Character1 = ASCII(SUBSTRING(WierdData,1,1)),
Character2 = ASCII(SUBSTRING(WierdData,2,1)),
Character3 = ASCII(SUBSTRING(WierdData,3,1))
FROM #MyHead
;




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search