Michael Peppler
Sybase Consulting
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
Sybase on Linux FAQ
Sybperl FAQ
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: Michael Peppler <mpeppler at peppler dot org>
Subject: Re: Different result from convert(varchar(255), a) and convert(a)
Date: Nov 24 2003 6:03PM

On Mon, 2003-11-24 at 09:18, Avis, Ed wrote:
> When selecting a 'real' column you get a different string value in
> Perl from 'select convert(varchar(255), a)' and 'select a':
>     $dbh->{AutoCommit} = 1;
>     $dbh->do('create table #a (a real not null)');
>     $dbh->do('insert into #a (a) values (.51645678281784058)');
>     my $sql = "select convert(varchar(255), a), a from #a";
>     my $sth = $dbh->prepare($sql);
>     $sth->execute();
>     while (my @r = $sth->fetchrow_array()) {
>         print "@r\n";
>     }
>     disconnect $dbh;
> This prints, on my system:
> .51645678281784058 0.516456782817841
> The trailing zero doesn't matter but the loss of precision is bad.  I
> know that 'real' is an approximate numeric type in Sybase but here
> Perl is giving me less precision than Sybase offers.

DBD::Sybase actually simply retrieves the REAL/FLOAT data item in native
format and stores it in the perl variable as number - it never is
converted to a char string until you print it out.

The rounding that you are seeing is probably happening in perl's print

while (my @r = $sth->fetchrow_array()) {        
    printf "%s %.20f\n", @r;    

gives me:

.51645678281784058 0.51645678281784057617

Of course all of these digits are approximations, so I don't think you
should make a big deal out of this...

Michael Peppler                              Data Migrations, Inc.       
Sybase T-SQL/OpenClient/OpenServer/C/Perl developer available for short or 
long term contract positions -