How can I optimize this query if given the following query returns either all entries in the table or entries that match only up to current date ? btw: The Query is targeted to a Oracle Linked Server on MS Sql 2005 as an Inline function.. Do not want this to be a table value function..
ALTER function [dbo].[ftsls031nnnHades](@withExpiredEntries bit =0)
 returns table as return    
select * 
  from openQuery(Hades ,"select '010' comno, 
                                trim(t$cuno) t$cuno,
                                trim(t$cpgs) t$cpgs,
                                t$dile,
                                t$qanp,
                                to_char(t$stdt,'dd Mon yy') t$stdt,
                                to_char(t$tdat,'dd Mon yy') t$tdat,
                                to_char(t$disc,'999.99') t$disc,
                                t$damt,
                                t$cdis,
                                t$gnpr,
                                t$refcntd,
                                t$refcntu 
                           from baan.ttdsls031010 
                          where (to_char(t$Tdat,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) 
                            and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd')) 
                         union all       
                         select '020' comno, 
                                trim(t$cuno) t$cuno,
                                trim(t$cpgs) t$cpgs,
                                t$dile,t$qanp,
                                to_char(t$stdt,'dd Mon yy') t$stdt,
                                to_char(t$tdat,'dd Mon yy') t$tdat,
                                to_char(t$disc,'999.99') t$disc,
                                t$damt,
                                t$cdis,
                                t$gnpr,
                                t$refcntd,
                                t$refcntu 
                           from baan.ttdsls031020 
                          where (to_char(t$tdAt,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd')) 
                            and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd'))  ")
p.s: Column naming conventions may be alien to those who are of non BaaN .. Please excuese me for bringing up 'BaaN' conventions into StackOverflow.