如何使用DATEDIFF? 有多少天两个日期内(How to use DATEDIFF? How

2019-09-28 21:21发布

如何使用DATEDIFF? 我怎样才能让这个工作? 或者我应该使用DATEDIFF完全地不同?

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

我试图让答案,多少天都在里面两个日期。

我想获得一个aswer这样的:
持续时间= 7天;

我有这样的数据库:

入门| 会停
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26

Answer 1:

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


Answer 2:

您是否获得一个NULL结果呢? 你必须在单引号中的列名在查询中,这意味着要传递的字符串'Started ''will_end'DATEDIFF ,而不是列的值。 尝试删除单引号,你将开始看到一些成果:

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';


Answer 3:

更换顺序

DATEDIFF( 'will_end', '入门')



Answer 4:

我认为有3个参数DATEDIFF传递(日期部分,开始日期,结束日期)

所以你的代码将是DATEDIFF(DD, '开始', 'will_end')

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



文章来源: How to use DATEDIFF? How many days are inside of two dates