Solved

MS T-SQL how to convert an nvarchar to hex and then to integer?

Posted on 2006-11-20
6
4,932 Views
Last Modified: 2012-05-05
I am using MS SQL-7
I am getting the error: Syntax error converting the nvarchar value '41801d37' to a column of data type int.
My question in summary is: How to convert a data of type "nvarchar" that represent a hex value to an integer vlaue?
In other words how to convert 41801d37 to 1098915127

*****************************************************
Now I am going to provide some details:
I have a table with a column called "hexcode"
The data type in this column is varbinary with a length of 12, and looks like this:
0x1A000000FCFF12000200371D8041591B403C381D8041591B403C
I needed to get rid of the leading 23 characters, and I did it like this:
SELECT SUBSTRING(master.dbo.fn_varbintohexstr(hexcode),23,32)
That gives me the following:371D8041591B403C381D8041591B403C

I use more string operations as follows:

select  HexCode,
          Convert(int,(Lo1+Lo2+Lo3+Lo4)) as NewIntCode
from
(
SELECT ObjectIdHi, objectIdLo, uiName, HexCode,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 7, 2) Lo1,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 5, 2) Lo2,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 3, 2) Lo3,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 1, 2) Lo4
FROM MyTable
)
as NewTable

My problem is that when I run the query I get the error:
Syntax error converting the nvarchar value '41801d37' to a column of data type int.






0
Comment
Question by:novice12
  • 4
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17980303
Give this a try:

select  HexCode,
          Convert(int,cast((Lo1+Lo2+Lo3+Lo4) as varbinary)) as NewIntCode
from
(
SELECT ObjectIdHi, objectIdLo, uiName, HexCode,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 7, 2) Lo1,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 5, 2) Lo2,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 3, 2) Lo3,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 1, 2) Lo4
FROM MyTable
)
as NewTable
0
 

Author Comment

by:novice12
ID: 17980366
When I run the above query, I don't get any errors. But the value of the output is wrong. If for example the hex value is 41801d37, the output I am getting is 855652096, when the right integer value is 1098915127.
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17980714
OK - a bit of a challenge. Instead of writing all of the base converion myself (brain is already tired) I googled a bit - from http://www.tek-tips.com/viewthread.cfm?qid=1192627&page=1

Creat a function in your database as below:

create function base2int( @num varchar(16), @radix tinyint )
returns bigint
as
begin
    declare @ret bigint; set @ret = 0
    declare @chars char(16); set @chars = '0123456789ABCDEF'
    declare @base bigint; set @base = 1
    declare @i tinyint; set @i = len(@num)
    declare @pos tinyint

    while @i >= 1 and @ret is not null
    begin
        set @pos = charindex(substring(@num, @i, 1), @chars)
        if @pos = 0
            set @ret = null
        else
            set @ret = @ret + @base * (@pos-1)

        set @i = @i-1
        set @base = @base * @radix
    end

    return @ret
end
go

Then
select  HexCode,
          dbo.base2int((Lo1+Lo2+Lo3+Lo4),16) as NewIntCode
from
(
SELECT ObjectIdHi, objectIdLo, uiName, HexCode,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 7, 2) Lo1,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 5, 2) Lo2,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 3, 2) Lo3,
Substring(substring(master.dbo.fn_varbintohexstr(HexCode),23,8), 1, 2) Lo4
FROM MyTable
)
as NewTable


That should work fine.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:novice12
ID: 17980784
Thanks for the help. I am using SQL 7. So I will have to convert it to a stored procedure.
0
 

Author Comment

by:novice12
ID: 17980897
Please disregard my last comment, it's actually SQL 2000. Sorry for the confusion, I will try the above function as you suggested. Thanks
0
 

Author Comment

by:novice12
ID: 17980951
The answer works perfectly. I googled a lot, but this is the best answer by far. Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
data stored like ????? in sql server column 19 54
tempdb log keep growing 7 33
SQL Server How-To Show Notes In First Row of Results 4 30
What is this datetime? 1 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question