SQL in LINUX. Variable or Database

Dear Friends :-),

Need your help once again.

I have following SQL in a shell script to pick values from database

select date_stamp, time_stamp from logs
where date=31012012

However, at times we need to take these value(s) from a variable. To do this we have following query.

select $date_stamp, $time_stamp from logs
where date-31012012

Now I want to write this SQL such a way that it will check if the variable is null then it will pickup value from database, and if variable is not null then it will pick it from the variable.

Please guide me to do it efficiently as I got to do it for lacs of records.

Thanks
Anu.

Check if variable is null or not .

if [ -z $date_stamp -a -z $time_stamp ]
then
   echo " variable is null , selecting value from database"
   # select date_stamp, time_stamp from logs where date=31012012
fi

The previous solution might falter in some shell versions. A small change to it:

if [ -z "$date_stamp" -a -z "$time_stamp" ]
then
   echo " variable is null , selecting value from database"
   # select date_stamp, time_stamp from logs where date=31012012
fi