Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All ,

I have one binary input parameter (@Binval BINARY(32) )in SP
which accept value like


0x0101010101010100000000000000000000000000000000000000000000000000

Now I want to remove 0x from above value so that final value will be

0101010101010100000000000000000000000000000000000000000000000000

and want to concatenate this with some string like

SELECT 'Your binary data is : ' + @Binval

where @Binval is input parameter for SP which has Hex value

How to do this in sql
Posted

After converting binary, try this-

SQL
select  'Your binary data is : ' + SUBSTRING( convert(nvarchar(32), @Binval,1),3,LEN(convert(nvarchar(32), @Binval,1)))

Or
SQL
Declare @binString nVarchar(32)
Set @binString=convert(nvarchar(32), @Binval,1) -- 
select  'Your binary data is : ' + SUBSTRING( @binString,3,LEN(@binString)) 


Converting binary value:
SQL
@binString=convert(nvarchar(32), @Binval,0)
print @binString
@binString=convert(nvarchar(32), @Binval,1)
print @binString
@binString=convert(nvarchar(32), @Binval,2)
print @binString


Out Put of converted string :
Your binary data is : ā
Your binary data is : 010101010101010000000000000000
Your binary data is : 010101010101000000000000000000
 
Share this answer
 
Comments
RDBurmon 28-Feb-12 7:26am    
No luck

I ran below query

declare @RE AS binary(32)
Set @RE =0x0101010101010100000000000000000000000000000000000000000000000000
SELECT SUBSTRING( convert(nvarchar(32), @RE,1),3,LEN(convert(nvarchar(32), @RE,2)))

and got below result
a
N Haque 28-Feb-12 7:34am    
You use 1 not 2 as 3rd Parameter.
Like the following-
declare @RE AS binary(32)
Set @RE =0x0101010101010100000000000000000000000000000000000000000000000000
SELECT SUBSTRING( convert(nvarchar(32), @RE,1),3,LEN(convert(nvarchar(32), @RE,1)))

I have got The following result :-
010101010101010000000000000000
RDBurmon 28-Feb-12 7:44am    
I guess you are using SQL 2008 , but I am using SQL2005
You try this -

SQL
select  'Your binary data is : ' + SUBSTRING(@Binval,3,LEN(@Binval))
 
Share this answer
 
Comments
RDBurmon 28-Feb-12 5:45am    
I think you have just posted with out executing it. Because above way I already known and that is never useful

finally expected result :

An error occurred
The data types varchar and varbinary are incompatible in the add operator.
N Haque 28-Feb-12 7:00am    
Sorry You have to convert the binary to String
Try it -



select 'Your binary data is : ' + SUBSTRING( convert(nvarchar(32), @Binval,1),3,LEN(convert(nvarchar(32), @Binval,1)))

OR

Declare @binString nVarchar(32)
Set @binString=convert(nvarchar(32), @Binval,1)
select 'Your binary data is : ' + SUBSTRING( @binString,3,LEN(@binString))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900