比较SQL查询中的两个日期给出错误-Codeigniter查询生成器

时间:2018-08-20 19:31:55

标签: sql-server codeigniter-3

这是我的代码:

function get_late_jobs($date1, $date2) {
    $this->db->select('Delivery.Packlist, Delivery.Job, Delivery.Promised_Date, Delivery.Shipped_Date, Job.Customer');
    $this->db->join('Job', 'Delivery.Job = Job.Job', 'inner');
    $this->db->where('Delivery.Promised_Date <', 'Delivery.Shipped_Date');
    $this->db->where('Delivery.Job IS NOT NULL', NULL);
    $this->db->where('Delivery.Shipped_Date IS NOT NULL', NULL);
    $this->db->where('Delivery.Shipped_Date >=', $date1);
    $this->db->where('Delivery.Shipped_Date <=', $date2);
    $this->db->order_by('Delivery.Shipped_Date');
    $query = $this->db->get( 'Delivery' );
    return $query->result_array();
}

我不断收到此错误:

错误号:22007/241

[Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]从字符串转换日期和/或时间时转换失败。

SELECT 
  "Delivery"."Packlist", 
  "Delivery"."Job", 
  "Delivery"."Promised_Date", 
  "Delivery"."Shipped_Date", 
  "Job"."Customer" 
FROM "Delivery" 
INNER JOIN "Job" 
  ON "Delivery"."Job" = "Job"."Job" 
WHERE 
  "Delivery"."Promised_Date" < 'Delivery.Shipped_Date' 
  AND "Delivery"."Job" IS NOT NULL 
  AND "Delivery"."Shipped_Date" IS NOT NULL 
  AND "Delivery"."Shipped_Date" >= '2018-08-08' 
  AND "Delivery"."Shipped_Date" <= '2018-08-15' 
ORDER BY 
  "Delivery"."Shipped_Date"

文件名:C:/inetpub/wwwroot/portalci/system/database/DB_driver.php

行号:691

我已经在SQL Management Studio中测试了该查询,并且该查询是从已存在的旧程序php页面获取的精确副本。

1 个答案:

答案 0 :(得分:0)

我建议更换

'Delivery.Shipped_Date'

使用

"Delivery"."Shipped_Date"

前者是不能转换为日期的字符串常量。