我现在写的一个.asp文件里面调用数据库的语句:SQL = SQL & " select equip_no," & chr(10)for i = 0 to Cint(to_date-from_date)
SQL = SQL & " round(SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0))/SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty)),4)*100 AS count_"& i &" ,SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0)) as out_"& i &",SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty,0)) as in_"& i &", " & chr(10)
str1= str1 + "decode(round(SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0))/SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty)),4)*100,NULL,0,round(SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0))/SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty)),4)*100) +" & chr(10)
str2= str2 + "decode(round(SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0))/SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty)),4)*100,NULL,0,1) +" & chr(10)
    str3= str3 + "SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),out_qty,0))+" & chr(10)
str4= str4 + "SUM(DECODE(substr(start_time,1,8),TO_CHAR(TO_DATE('"& from_date &"','YYYYMMDD')+"& i &",'YYYYMMDD'),in_qty,0))+" & chr(10)
next
SQL = SQL &"(" &str1 & " 0)/ ("& str2 &" 0) as count_sum,("& str3 &" 0) as out_qty,("& str4 &" 0) as in_qty" & chr(10)
SQL = SQL & " from TST_LIS" & chr(10)
SQL = SQL & " where substr(start_time,1,8) between '"& from_date &"' and '"& to_date &"' " & chr(10)
        if job <> "ALL" then
SQL = SQL & " and production_type in ("
for vRowCount = 0 to com_job.RowCount - 1
if com_job.getColumn(vRowCount,"chk") = "1" then
SQL = SQL & "'" & com_job.getColumn(vRowCount,"job_code") & "',"
end if
next
SQL = SQL & " '')"
end if
if tech <> "ALL" then
SQL = SQL & " and production_type1= '"& tech &"' " & chr(10)
end if
SQL = SQL & " group by equip_no" & chr(10)

执行以后我查看日志:
发现在数据中运行的where语句:where substr(start_time,1,8) between '20101031' and '20101106'
它直接把这两个日期值相减了~所以出来75天
而实际10月31到11月6号只有7天~这个有什么办法可以解决?

解决方案 »

  1.   

    注意里面的拼接 特别是引号问题
    start_time存的是什么的格式日期where to_number(substr(trim(start_time),1,8)) between 20101031 and 20101106--orwhere to_date(substr(trim(start_time),1,8),'yyyymmdd') 
    between to_date('20101031','yyyymmdd') and to_date('20101106','yyyymmdd')
      

  2.   

    oracle 如果数字字符的话,在处理的时候会默认的将字符转换为数字。
    你现在用的时间比较,而不是数字比较,所以你必须使用to_date进行转换。