时间段查询,急用,拜托了!
要通过区域代码来查询特定区域的人在一段时间内的购气情况,代码该怎么写啊。我写了两个怎么都错的?
第一种这样,有错误,提示找不到Date1
AnsiString sql;
sql = "select * from customer where khrq between 'Date1 ' and 'Date2 ' ";
Query1-> Close();
Query1-> SQL-> Add(sql);
Query1-> ParamByName( "Date1 ")-> AsDate=DateTimePicker1-> Date;
Query1-> ParamByName( "Date2 ")-> AsDate=DateTimePicker2-> Date;
Query1-> Open();
第二种是:三个条件都不管用,什么都查不出来么。哪位帮忙给看下啊,十分感谢!
AnsiString sql;
sql = "select * from customer ";
AnsiString szFilter;
if(Edit1-> Text.Length()> 0)
{
szFilter = "gqrq > = ' " + StrToDateTime(Edit1-> Text) + " ' ";
}
if(Edit2-> Text.Length()> 0)
{
if(szFilter.Length()> 0)
szFilter += " and ";
szFilter = "gqrq <= ' " + StrToDateTime(Edit2-> Text) + " ' ";
}
if(ComboBox1-> Text.Length()> 0)
{
szFilter = " and qydm like ' " + ComboBox1-> Text + "% ' ";
}
if(szFilter.Length()> 0)
sql += szFilter;
Query1-> SQL-> Clear();
Query1-> SQL-> Add(sql);
Query1-> Open();
[解决办法]
sql = "select * from customer where khrq between ' "+DateTimePicker1-> Date.DateString()+ " 'and ' "+DateTimePicker2-> Date.DateString()+ " ' ";
[解决办法]
第一种:你的date要加:号如 ':Date1 '
你把最终生所的SQL 语句打出来看看
[解决办法]
你的参数写法不对
sql = "select * from customer where khrq between 'Date1 ' and 'Date2 ' ";
参数前要加:号
sql = "select * from customer where khrq between ':Date1 ' and ':Date2 ' ";
还有如果直接把TDate当成参数就不需要 '单引号了
sql = "select * from customer where khrq between :Date1 and :Date2 ";
也可以直接用字符串比较,那就不用转为TDateTime了,但要视数据库而定