Hi,
I'm looking for a function to convert a character to hexadecimal. E.g., 7 ->
37
Can anyone help pls ?
TIA !Desmond wrote:
> Hi,
> I'm looking for a function to convert a character to hexadecimal. E.g., 7
->
> 37
> Can anyone help pls ?
> TIA !
This works for the ASCII characters (0-255):
DECLARE @.c CHAR(1);
SET @.c = '7';
PRINT
SUBSTRING('0123456789ABCDEF',ASCII(@.c)/16+1,1)+
SUBSTRING('0123456789ABCDEF',ASCII(@.c)%1
6+1,1);
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||I'd created the function as below :
CREATE FUNCTION fn_Ascii2Hex (@.InputAscii varchar(2))
RETURNS int
AS
BEGIN
return SUBSTRING('0123456789ABCDEF',ASCII(@.Inpu
tAscii)/16+1,1) +
SUBSTRING('0123456789ABCDEF',ASCII(@.Inpu
tAscii)%16+1,1);
END
However, I got :
Server: Msg 195, Level 15, State 10, Line 1
'fn_Ascii2Hex' is not a recognized function name.
when I try to test the function using "select fn_Ascii2Hex(f1) from tab1;"
f1 is a varchar(2) column.
Advice please. Tia.
"David Portas" wrote:
> Desmond wrote:
> This works for the ASCII characters (0-255):
> DECLARE @.c CHAR(1);
> SET @.c = '7';
> PRINT
> SUBSTRING('0123456789ABCDEF',ASCII(@.c)/16+1,1)+
> SUBSTRING('0123456789ABCDEF',ASCII(@.c)%1
6+1,1);
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||did you try prefixing your function call with the owner name?
i.e.
select dbo.fn_Ascii2Hex(f1) from tab1
"Desmond" <Desmond@.discussions.microsoft.com> wrote in message
news:2CA29BBC-7988-4FD2-BA01-63AC829E943E@.microsoft.com...
> I'd created the function as below :
> CREATE FUNCTION fn_Ascii2Hex (@.InputAscii varchar(2))
> RETURNS int
> AS
> BEGIN
> return SUBSTRING('0123456789ABCDEF',ASCII(@.Inpu
tAscii)/16+1,1) +
> SUBSTRING('0123456789ABCDEF',ASCII(@.Inpu
tAscii)%16+1,1);
> END
> However, I got :
> Server: Msg 195, Level 15, State 10, Line 1
> 'fn_Ascii2Hex' is not a recognized function name.
> when I try to test the function using "select fn_Ascii2Hex(f1) from tab1;"
> f1 is a varchar(2) column.
> Advice please. Tia.
> "David Portas" wrote:
>
E.g., 7 ->|||Oh ! Now it works after I prefix it with dbo !
Many thanks Jims !
"Jim Underwood" wrote:
> did you try prefixing your function call with the owner name?
> i.e.
> select dbo.fn_Ascii2Hex(f1) from tab1
> "Desmond" <Desmond@.discussions.microsoft.com> wrote in message
> news:2CA29BBC-7988-4FD2-BA01-63AC829E943E@.microsoft.com...
> E.g., 7 ->
>
>
No comments:
Post a Comment