使用 datediff 来 return 至少相差两天的记录
use datediff to return records with at leats two days difference
我确实有一个 SQL Anywhere table,日期字段名为 "fecha_inicio",该字段的有效值为 12/12/2017,现在我需要创建一个从 table 中获取所有记录的语句,其中 fecha_inicio 至少比当前日期早两天,这就是我目前所做的
AND ((DATEDIFF(day, fecha_inicio, cast(getdate()as date)<=2)
但它没有 return 任何结果或抛出任何异常,你能帮我改进这个声明吗
这是整句话
"SELECT numero_orden, horario, accion_formativa FROM DBA.orden_inicio_af WHERE (numero_orden_inicio = ?) AND ((DATEDIFF(day, fecha_inicio, cast(getdate()as date)<=2) AND (estado_orden_inicio = '01' OR estado_orden_inicio = '02' OR estado_orden_inicio = '03') AND (nit = ?)"
不要在列上使用 datediff()
。相反,直接与 getdate()
进行比较。这对优化器来说要好得多。
我想你打算:
where fecha_inicio < dateadd(day, -2, cast(getdate() as date))
我确实有一个 SQL Anywhere table,日期字段名为 "fecha_inicio",该字段的有效值为 12/12/2017,现在我需要创建一个从 table 中获取所有记录的语句,其中 fecha_inicio 至少比当前日期早两天,这就是我目前所做的
AND ((DATEDIFF(day, fecha_inicio, cast(getdate()as date)<=2)
但它没有 return 任何结果或抛出任何异常,你能帮我改进这个声明吗
这是整句话
"SELECT numero_orden, horario, accion_formativa FROM DBA.orden_inicio_af WHERE (numero_orden_inicio = ?) AND ((DATEDIFF(day, fecha_inicio, cast(getdate()as date)<=2) AND (estado_orden_inicio = '01' OR estado_orden_inicio = '02' OR estado_orden_inicio = '03') AND (nit = ?)"
不要在列上使用 datediff()
。相反,直接与 getdate()
进行比较。这对优化器来说要好得多。
我想你打算:
where fecha_inicio < dateadd(day, -2, cast(getdate() as date))