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


Converting Hexadecimal String Values to Alpha (ASCII) Strings


Converting Hexadecimal String Values to Alpha (ASCII) Strings

Author
Message
JH Park
JH Park
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 12
Thank you Jeff, I was too sleepy to run comparison tests myself last night.
Your input length condition is a nice touch.
But no Jeff, it not a cross-join nor generates 65,536 internal rows. It's a n inner join. You can see it if you check out its execution plan, and it can be replaced with:
----------------------------------------
select @output = @output + char(b.dec)
from dec2hex a inner join dec2hex b
on b.hex = substring(@input,a.dec*2+1,2)
where a.dec*2+1 < LEN(@input)
order by a.dec
----------------------------------------
without affecting its semantics at all.
And dec2hex table is looked-up just LEN(@output) times.(It was 256 times before you add the criteria, though.)

I think it's weird that Richard's function is still much faster. I thought the join routine of the server should be most efficient. Moreover, my function calls substring() half the times of Richard's.

Anyway, it was a very interesting quiz. Thank you all!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84345 Visits: 41061

Heh... and I was too sleepy this morning... I shouldn't post when I'm pooped...

It is still a bit of a half cross join (more specifically, a triangular join and does an index scan of 76 rows) but it certainly isn't 65,536. My mistake. That would also explain why Richard's is still faster...



--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
Eddie Lee
Eddie Lee
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 18

Another function for the mix:

if objectproperty (object_id ('dbo.fn_HexToAlpha'), 'istablefunction') = 0 drop function dbo.fn_HexToAlpha
go
create function dbo.fn_HexToAlpha (@HexString char (40))
returns varchar (20) as
begin
declare @Loop tinyint, @AsciiString varchar (20), @Nibble1 char (1), @Nibble2 char (1)
select @AsciiString = '', @Loop = 1
while @Loop < len (@HexString)
begin
select @Nibble1 = upper (substring (@HexString, @Loop, 1)), @Nibble2 = upper (substring (@HexString, @Loop + 1, 1))
set @AsciiString = @AsciiString + char (16 * case @Nibble1 when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else @Nibble1 end + case @Nibble2 when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else @Nibble2 end)
set @Loop = @Loop + 2
end
return @AsciiString
end
go
select dbo.fn_HexToAlpha (HexString) from #HexToAlpha

Eddie Lee.





Eddie Lee
Eddie Lee
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 18

I ran mine and Richard's functions on Jeffs test data (pushed up to 1000000 rows) and my function above (at 00:01:25.603) just pipped Richards (at 00:01:57.740).

alter table #HexTest add ASCIIValue varchar (20)
go


if object_id ('tempdb.dbo.#TimeTrial') is not null drop table #TimeTrial
create table #TimeTrial (Event varchar (20), DateStamp datetime default getdate ())

update #HexTest set ASCIIValue = null -- reset
insert #TimeTrial (Event) values ('RichardStart')
update #HexTest set ASCIIValue = dbo.f_hextostr (HexValue)
insert #TimeTrial (Event) values ('RichardFinish')
select * from #HexTest

update #HexTest set ASCIIValue = null -- reset
insert #TimeTrial (Event) values ('EddieStart')
update #HexTest set ASCIIValue = dbo.fn_HexToAlpha (HexValue)
insert #TimeTrial (Event) values ('EddieFinish')
select * from #HexTest

select (select DateStamp from #TimeTrial where Event = 'RichardFinish') - (select DateStamp from #TimeTrial where Event = 'RichardStart') Richard
select (select DateStamp from #TimeTrial where Event = 'EddieFinish') - (select DateStamp from #TimeTrial where Event = 'EddieStart') Eddie

Eddie.





Richard Dragossy-168541
Richard Dragossy-168541
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 31

Hey Eddie!

I like this conversation very much!

Try this with only 100.000 recs!

if object_id ('tempdb.dbo.#TimeTrial') is not null drop table #TimeTrial
create table #TimeTrial (id integer identity(1,1), Event varchar (20), datestart datetime default getdate (), dateend datetime null)

declare @counter integer
set @counter=1
while @counter<13
begin
insert #TimeTrial (Event) values ('Richard')
update HexTest set ASCIIValue = dbo.f_hextostr (HexValue)
update #TimeTrial set dateend=getdate() where id=@counter
set @counter=@counter+1

insert #TimeTrial (Event) values ('Eddie')
update HexTest set ASCIIValue = dbo.fn_HexToAlpha (HexValue)
update #TimeTrial set dateend=getdate() where id=@counter
set @counter=@counter+1
end

select *, dateend-datestart as diff from #TimeTrial order by id

The result:

1 Richard 00:00:17.333
2 Eddie 00:00:08.283
3 Richard 00:00:06.610
4 Eddie 00:00:08.010
5 Richard 00:00:06.420
6 Eddie 00:00:08.000
7 Richard 00:00:06.520
8 Eddie 00:01:02.360
9 Richard 00:00:06.460
10 Eddie 00:00:07.780
11 Richard 00:00:06.510
12 Eddie 00:00:07.803

Conclusions: The first runs (1-2) must be eliminated, our data not in cash yet. The runs 7-8 must be eliminated, Your result totally differs from others ( really sometimes I don't know, what the hell is running on background), the rest results are comparable.

By the way: our functions are able to run faster, if:

You eliminate the UPPER function (suppose the HEX string is correct);

I eliminate the IF part (suppose the HEX string is correct)

Best regards: Richard


Eddie Lee
Eddie Lee
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 18

Richard yes, your loop does prove your fn to be a bit quicker. Weird how the run time of mine fluctuates so?!

Ho hum.

Good chatting.

Regards, Eddie.





Dennis D. Allen
Dennis D. Allen
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 163
I have found using STUFF to create the output to be a little bit faster. I am assuming that it is due to traditional string concantination issues...



ALTER FUNCTION hex2alpha( @input VARCHAR(256) ) RETURNS VARCHAR(256) AS
BEGIN
DECLARE
@output VARCHAR(256); SET @output = SPACE(LEN(@input)/2)

SELECT @output = STUFF(@output,a.dec+1,1,CHAR(b.dec))
FROM
dec2hex AS a
INNER JOIN dec2hex AS b ON b.hex = SUBSTRING(@input,a.dec*2+1,2)
WHERE
a.dec < LEN(@input)/2
ORDER BY a.dec

RETURN @output
END

Lashams
Lashams
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 48

Firstly my apologies for not responding sooner

Secondly, thank you all for the marvellous responses and ensuing competition. Sadly I must now say my original SQL was a very poor contender over 1,000,000 records, and some of the alternatives, well let's just say WOW!

I have really learned some stuff from you people, so much appreciated.

Stephen





Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84345 Visits: 41061
Thanks for coming back with that... lots of us end up wondering if we actually helped or confused. Really appreciate the feedback even if it is a bit later than most.

--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
mike brockington
mike brockington
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 245
Surely the real lesson from this should be that if something is this hard in SQL you should be doing it in a different language?
I rarely come across an application that is written purely in SQL, so there is usually a presentation layer that is more capable of handling what looks like a purely presentational issue.

Throw away your pocket calculators; visit www.calcResult.com


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