mpeppler/DBD-Sybase

Loss of precision with Money datatype

Closed this issue · 2 comments

Copied from http://www.peppler.org/bugdb/view?bug_id=664

When working with money data type I am getting the output only upto two decimals
places when using DBI and DBD::Sybase.
Is their a setting which can give me the output upto 4 decimals just like isql.

The code is :

#!/usr/bin/perl

use DBI;
use DBD::Sybase;

$dbh = DBI->connect("dbi:Sybase:", $ENV{"LOGNAME"}, "");
$sth = $dbh->prepare("
create table #t (mm money, ii decimal(10,4))
insert into #t(mm, ii) values(1234.4485, 45678.9898)
select mm, ii from #t
");
$sth->execute;

@Row = $sth->fetchrow_array();
print join("|", @Row), "\n";

output is :
1234.45|45678.9898

I can use the convert function it gives the correct output but in generic
programs using select * from table is more efficient then select col1, col2
..... from table.

modified program

#!/usr/bin/perl

use DBI;
use DBD::Sybase;

$dbh = DBI->connect("dbi:Sybase:", $ENV{"LOGNAME"}, "");
$sth = $dbh->prepare("
create table #t (mm money, ii decimal(10,4))
insert into #t(mm, ii) values(1234.4485, 45678.9898)
select convert(decimal(10, 4), mm), ii from #t
");
$sth->execute;

@Row = $sth->fetchrow_array();
print join("|", @Row), "\n";

output is :
1234.4485|45678.9898

isql actually returns the rounded value with 2 decimals:

[mp@li ~]$ isql -Usa -Prunning -w1024
1> create table #t (mm money, ii decimal(10,4))
insert into #t(mm, ii) values(1234.4485, 45678.9898)
select mm, ii from #t2> 3>
4> go
(1 row affected)
mm ii


             1,234.45    45678.9898 

(1 row affected)
1>

FWIW: Sybase open client converts a "money" datatype to char with 2 decimals. FreeTDS converts it with 4 decimals.