Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use DBI to retrieve/update data from MS SQL Server and Win2000. To my dismay, the tables have numerous 8 byte binary data types (which contain data such as 0x02BE8B003DB14301) which don't seem to output at all when i do a simple select statement--perl outputs strange ascii characters. all the other columns that are not binary output fine. how would i get perl to handle this type of data correctly? michael

Replies are listed 'Best First'.
Re: DBI and binary data type
by Juerd (Abbot) on Mar 14, 2002 at 19:42 UTC

    That (the 0x02BE... form) is just a textual representation for the actual data (the "stange" characters). To get it in Perl, you could use:

    my $hexform = '0x' . unpack "H*", $binary;
    HTH

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: DBI and binary data type
by mpeppler (Vicar) on Mar 14, 2002 at 20:48 UTC
    Which DBD driver are you using?

    Are the binary data items "timestamp" columns?

    Michael

Re: DBI and binary data type
by Anonymous Monk on Mar 14, 2002 at 21:53 UTC
    thanks for your response juerd and mpeppler. i'm using DBD::ODBC. The unpack worked very nicely, but this leads me to another problem--how to perform an insert into a binary column with this retrieved binary number. i'm trying like this:
    $ctsth = $ctdbh->prepare( "INSERT INTO table(ParentID,ChildID) VALUES +(?,?)" ); $ctsth->bind_param( 1, $hexform1, {TYPE => SQL_BINARY} ); $ctsth->bind_param( 2, $hexform2, {TYPE => SQL_BINARY} ); $ctsth->execute();
    but the server keeps telling me that it can't implicitly convert a varchar to a binary and to use the SQL CONVERT function. i've tried using the CONVERT function in query analyzer and it gives me strange results
    declare @var varchar(8) set @var='0x02BE8B003DB14301' select convert(binary(8), @var)
    returns 0x3078303242453842 and not 0x02BE8B003DB14301. how can i insert this binary number? thanks, Michael