带有SQL查询的DateDiff

时间:2013-01-25 23:28:20

标签: sql datediff

我想知道是否有人可以通过以下代码指出问题。特别是dateDiff部分。我想要做的是弄清楚发票和存款日期之间的差异(以天为单位)。如果存款日期为空,我想使用今天的日期。很简单,我想过......

$rs = mysql_query("SELECT i.dateSent
    , i.dateDeposit
    , i.invoiceID
    , i.gstValue
    , i.invoiceValue AS invoiceTotal
    , p.projectValue
    , i.invoiceNumber
    , p.projectName
    , p.projectNumber
    , p.projectID
    , c.clientName
    , c.clientCode
    , c.clientID
    , DATEDIFF(i.dateSent, CASE WHEN i.dateDeposit IS Null Then GetDate() ELSE i.dateDeposit) as numbDays

FROM cms_invoices i

LEFT JOIN cms_projects p ON p.projectID = i.projectID
LEFT JOIN cms_clients c ON p.clientID = c.clientID

ORDER BY i.dateDeposit ASC, c.clientName ASC
");

在此阶段,结果没有结果。 dateDiff行会中断查询。精氨酸。

谢谢, @rrFive

1 个答案:

答案 0 :(得分:2)

好像你错过了END

... DATEDIFF(i.dateSent, 
    CASE
        WHEN i.dateDeposit IS NULL
            THEN GetDate()
        ELSE i.dateDeposit
     END) AS numbDays