SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Varbinary to Varchar
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cstokes91
Yak Posting Veteran

USA
58 Posts

Posted - 07/09/2013 :  12:38:48  Show Profile  Reply with Quote
Hey guys,

I am working with some data that is in the format varbinary(72). I am trying to convert that to a readable text but for some reason both cast and convert are not working.

Any ideas?

Here is some sample data and the codes I've tried.


select id, comment1, cast(comment1 as varchar(max)) as comment1varchar
from database..table
where comment1 <> '' 


AND


select id, comment1, convert(varchar(5000),comment1, 0) as comment1varchar
from database..table
where comment1 <> '' 


Here is one of varbinary entries:
0x2900544F204245205345525645442046524F4D20383A30302020412E4D2E20544F20393A303020502E4D2E

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1050 Posts

Posted - 07/09/2013 :  13:14:41  Show Profile  Reply with Quote
what do you mean by Not Working? is it throwing error? or ?

Cheers
MIK
Go to Top of Page

cstokes91
Yak Posting Veteran

USA
58 Posts

Posted - 07/09/2013 :  13:58:08  Show Profile  Reply with Quote
The return is either blank or gives one random symbol or letter.
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1050 Posts

Posted - 07/09/2013 :  14:37:11  Show Profile  Reply with Quote
check with this ?

convert(varchar(5000),comment1, 1)

Cheers
MIK
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 07/10/2013 :  03:37:05  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Two first bytes are length information.
DECLARE	@Data VARBINARY(MAX) = 0x2900544F204245205345525645442046524F4D20383A30302020412E4D2E20544F20393A303020502E4D2E;
 
SELECT	CAST(SUBSTRING(@Data, 3, LEN(@Data)) AS VARCHAR(MAX))



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

cstokes91
Yak Posting Veteran

USA
58 Posts

Posted - 07/15/2013 :  11:00:36  Show Profile  Reply with Quote
Sorry for the delayed response... THANK YOU SwePeso!! :D

That worked and can finally get it out.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000