if($number eq "all") { $select = ''; if($fromdate || $todate) { $bet = "and CONVERT(CHAR(10),tdate,101) between '$fromdate' and '$todate'"; #$bet = "and CONVERT(CHAR(10),tdate,101) between '?' and '?'"; }else{ $bet = 'and tdate = (Select Max(tdate) From mytable) order by tdate desc'; } }else{ $select = "and number='$number'"; if($fromdate || $todate) { $bet = "and CONVERT(CHAR(10),tdate,101) between '$fromdate' and '$todate'"; #$bet = "and CONVERT(CHAR(10),tdate,101) between '?' and '?'"; }else{ $bet = 'order by tdate desc'; } } my $all = join ( ',', ('?') x @list ); my $sql = $dbh->exec_select( "SELECT tdate,account,name FROM mytable WHERE number in ($all) $select $bet ", @list); #my $sql = $dbh->exec_select( "SELECT tdate,account,name FROM mytable WHERE number in ($all) $select $bet ", @list, $fromdate, $todate);