1

如何使用 DATEDIFF?我怎样才能使它工作?还是我应该完全不同地使用 DATEDIFF?

SELECT DATEDIFF('Started ','will_end') AS 'Duration' FROM my_table WHERE id = '110';

我试图得到答案,两个日期之间有多少天。

我想得到一个答案:
Duration = 7 days;

我有这种数据库:

开始 | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26

4

4 回答 4

4

will_end第一,started第二:

SELECT  DATEDIFF('2009-12-24', '2009-12-17') 

---
  7

此外,从您的字段名称中删除单引号:

SELECT  DATEDIFF(will_end, started) AS Duration
FROM    my_table
WHERE   id = 110

,或用反引号替换它们:

SELECT  DATEDIFF(`will_end`, `started`) AS `Duration`
FROM    `my_table`
WHERE   `id` = 110
于 2009-12-22T13:54:48.490 回答
2

你得到NULL结果了吗?您的查询中有单引号中的列名,这意味着您传递的是字符串'Started ''will_end'toDATEDIFF而不是列值。尝试删除单引号,您将开始看到一些结果:

SELECT DATEDIFF(Started, will_end) AS Duration FROM my_table WHERE id = '110';

请注意,这会给您带来负面结果。要获得肯定的结果,请颠倒列的顺序:

SELECT DATEDIFF(will_end, Started) AS Duration FROM my_table WHERE id = '110';
于 2009-12-22T13:59:19.367 回答
0

更换订单

DATEDIFF('will_end','开始')

于 2009-12-22T13:55:53.793 回答
0

我认为在 DATEDIFF 中要传递 3 个参数( datepart , startdate , enddate )

所以你的代码是 DATEDIFF ( dd , 'Started ','will_end' )

http://msdn.microsoft.com/en-us/library/ms189794.aspx

于 2009-12-22T13:57:11.663 回答