这条关于动态日期查询的SQL语句怎么写? (50分)

一辉

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个数据库,其别名为tou,其中有一个字段tou_date,类型为Date
查询条件:例如从01/12/2000 到 02/25/2001
我用6个ComboBox控件分别代表2个查询区间年、月、日
ComboBox1、ComboBox4代表年
ComboBox2、ComboBox5代表月
ComboBox3、ComboBox6代表日
初步代码如下,但是报错,谁能帮我改一下?
select * from tou where tou_date >='ComboBox2.items/ComboBox3.items/ComboBox1.items'
and tou_date <='ComboBox5.items/ComboBox6.items/ComboBox4.items'
 
select * from tou where tou_date >='ComboBox2.text/ComboBox3.text/ComboBox1.text'
and tou_date <='ComboBox5.text/ComboBox6.text/ComboBox4.text'
或select * from tou where tou_date between 'ComboBox2.text/ComboBox3.text/ComboBox1.text'
and 'ComboBox5.text/ComboBox6.text/ComboBox4.text'
 
'ComboBox2.items/ComboBox3.items/ComboBox1.items'
中间均是字符串,当然报错了,试试
var s1,s2:String;

s1:=ComboBox2.text+'/'+ComboBox3.text+'/'+ComboBox1.text;
s2:=ComboBox5.text+'/'+ComboBox6.text+'/'+ComboBox4.text;

然后再select ...tou_date>s1 and toudate<s2
 
同意楼上的看法!
 
不行的,sql语句连串才能运行,至少应该接着写:

sqlstr: string;
sqlstr := select * from tou whre tou_date > ''' + s1 + '''and ' ......

或者用动态的
select ...tou_date > :s1 and toudate < :s2
 
用两个DateTimePicker简单很多阿
query1.SQL.add('select * from tou where tou_date >=:date1 and tou_date <=:date2');
query.parambyname(date1):=DateTimePicker2.date;
query.parambyname(date2):=DateTimePicker2.date;
 
完全同意楼上的说法,何必用这么多的ComboBox呢?
 
用两个DateTimePicker简的确很好,但是D5语法应如下写:
query1.SQL.add('select * from tou where tou_date >='+datetostr(DateTimePicker1.date) and tou_date <='+datetostr(DateTimePicker2.date) );
 
解决办法有两种:
1 使用datetimepicker控件
为了避免由于数据库和客户端使用日期格式的不同,使用字符型变量作为中间过渡。
示例如下(基于oracle):
Query1.sql := 'select * from tou where to_char(tou_date,''yyyymmdd'') >= ' +
#39 + FormatDateTime('yyyymmdd',DateTimePicker1.date) + #39 + ' and to_char(tou_date,''yyyymmdd'') <= ' +
#39 + FormatDateTime('yyyymmdd',DateTimePicker2.date) + #39;
2 继续使用combobox
将combobox中的text组合成4位年2位月2位日的字符串格式,按照上述的方式进行操作,
注意#39不能缺省。至于combobox中的内容组合你再想想办法,不是最初写的那么随意的。
 

Similar threads

顶部