Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to convert hex string values to varbinary?

Author  Topic 

waleed_cs2000
Starting Member

14 Posts

Posted - 2009-06-17 : 11:58:20
hi
I have this script to get the default value of a column dynamically ,it works for most columns types except for varbinary that has default hex value, when @t = 0x0343 ,then @v =0x03
note that I had to use nvarchar(4000) at first because INFORMATION_SCHEMA.COLUMNS forces me to do so, can you help me? thanks in advance


-----------------------------------
create table #temp
(
column_name sysname,
column_def nvarchar(4000)
)

insert into #temp SELECT column_name,column_default

FROM INFORMATION_SCHEMA.COLUMNS
where table_Name ='table_1'

declare @t nvarchar(4000)
set @t=(select column_def from #temp where column_name='c_varbin')
set @t = replace (@t ,'(','')
set @t = replace (@t ,')','')



select @t

declare @v varbinary
set @v = convert ( varbinary,@t)

select @v
drop table #temp
-----------------------------------


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-17 : 12:39:07
not sure if thats cause, but you've not specified length while convert . try giving length in last set statement

set @v = convert ( varbinary(10),@t)
Go to Top of Page

waleed_cs2000
Starting Member

14 Posts

Posted - 2009-06-18 : 03:35:05
no,it didn't work, but TG member has posted a solution in similar question in the topic
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127571

thanks visakh16 and TG
Go to Top of Page
   

- Advertisement -