views:

12400

answers:

3

I'm looking for a way to transform a genuine string into it's hexadecimal value in SQL. I'm looking something that is Informix-friendly but I would obviously prefer something database-neutral

Here is the select I am using now:

SELECT SomeStringColumn from SomeTable

Here is the select I would like to use: SELECT hex( SomeStringColumn ) from SomeTable

Unfortunately nothing is that simple... Informix gives me that message: Character to numeric conversion error

Any idea?

+3  A: 

can you try and use Cast and the fn_varbintohexstr?

SELECT master.dbo.fn_varbintohexstr(CAST(SomeStringColumn AS varbinary)) FROM SomeTable

I'm not sure if you have that function in your database system, it is in MS-SQL.

I just tried in in my SQL server MMC on one of my tables:

SELECT     master.dbo.fn_varbintohexstr(CAST(Addr1 AS VARBINARY)) AS Expr1
FROM         Customer

This worked as expected. possibly what I know as master.dbo.fn_varbintohexstr on MS-SQL, might be similar to informix hex() function, so possibly try:

SELECT     hex(CAST(Addr1 AS VARBINARY)) AS Expr1
FROM         Customer
stephenbayer
I think you answered the question he meant to ask. +1
colithium
Even in MSSQL this function is not supported/documented. Use of the function is not recommended if you require compatibility with future versions of MS SQL server
Faiz
+1  A: 

If it is possible for you to do this in the database client in code it might be easier.

Otherwise the error probably means that the built in hex function can't work with your values as you expect. I would double check the input value is trimmed and in the format first, it might be that simple. Then I would consult the database documentation that describes the hex function and see what its expected input would be and compare that to some of your values and find out what the difference is and how to change your values to match that of the expected input.

A simple google search for "informix hex function" brought up the first result page with the sentence: "Must be a literal integer or some other expression that returns an integer". If your data type is a string, first convert the string to an integer. It looks like at first glance you do something with the cast function (I am not sure about this).

select hex(cast SomeStringColumn as int)) from SomeTable
Josh
Unfortunately, you're solution would only work if the string is indeed a number which is not my case...
Shadow_x99
+1  A: 

The following works in Sql 2005.

select convert(varbinary, SomeStringColumn) from SomeTable
jhamm
Spot on. This should be the selected answer.
the.jxc