SQL Server DATE_DIFF INTERVAL?

时间:2015-07-29 10:22:43

标签: sql sql-server r sql-server-2014

你好,我做错了什么?

iam试图显示r.reizen与r.vertrek的日期差异为1天

有人可以提供帮助

SELECT DISTINCT b.vertrek, b.reiscode, r.bestemming, r.dagen, r.prijs
FROM boekingen b
INNER JOIN reizen r ON b.reiscode = r.reiscode
WHERE b.reiscode <> 'JH10' 
AND ((b.vertrek BETWEEN 
(SELECT MIN(DATE_SUB(vertrek, INTERVAL 1 DAY)) FROM boekingen WHERE          reiscode = 'JH10') AND 
(SELECT MIN(DATE_ADD(vertrek, INTERVAL 1 DAY)) FROM boekingen WHERE                reiscode = 'JH10')) OR 
(b.vertrek BETWEEN (SELECT MAX(DATE_SUB(vertrek, INTERVAL 1 DAY)) FROM            boekingen WHERE reiscode = 'JH10') 
AND (SELECT MAX(DATE_ADD(vertrek, INTERVAL 1 DAY)) FROM boekingen  WHERE reiscode = 'JH10')))
ORDER BY 1
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '1'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '1'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '1'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '1'.

2 个答案:

答案 0 :(得分:0)

简单回答:您正在使用Microsoft SQL Server引擎的DATE_SUB的MySQL语法。对于猜测的人来说,这是一个Microsoft SQL错误。

 Msg 102, Level 15, State 1 

您正在使用的日期功能是:

DATE_SUB(vertrek, INTERVAL 1 DAY))
DATE_ADD(vertrek, INTERVAL 1 DAY)) FROM 

Microsoft变种是:

DATEADD(DAY, -1, vertrek)
DATEADD(DAY, 1, vertrek)

Accepteer dit antwoord en punten toekennen,bedankt。

答案 1 :(得分:0)

除了修复日期算术之外,如果没有@@toolbar = nil子句中的子查询,您的查询将更容易阅读。将摘要移动到WHERE子句很简单:

FROM