如何使用DATEDIFF?两个日期之间有多少天

发布于 2024-08-16 11:27:37 字数 325 浏览 10 评论 0原文

如何使用DATEDIFF?我怎样才能让它发挥作用?或者我应该完全不同地使用 DATEDIFF 吗?

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

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

我想得到这样的问题:
持续时间 = 7 天;

我有这样的数据库:

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

How to use DATEDIFF? How can I make this to work? or should I use DATEDIFF completly differently?

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

I try to get answer, how many days are inside of two dates.

I would like to get an aswer like:
Duration = 7 days;

I have this kind of database:

Started | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

向日葵 2024-08-23 11:27:37

首先放置 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

Put will_end first, started second:

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

---
  7

Also, remove the single quotes from your field names:

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

, or replace them with the backticks:

SELECT  DATEDIFF(`will_end`, `started`) AS `Duration`
FROM    `my_table`
WHERE   `id` = 110
病毒体 2024-08-23 11:27:37

您得到的是 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';

Are you getting a NULL result? You have the column names in single quotes in your query, which means you are passing the strings 'Started ' and 'will_end' to DATEDIFF rather than the column values. Try removing the single quotes, and you will start to see some results:

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

Note that this will give you a negative result. To get a positive result, reverse the order of the columns:

SELECT DATEDIFF(will_end, Started) AS Duration FROM my_table WHERE id = '110';
把时间冻结 2024-08-23 11:27:37

替换订单

DATEDIFF('will_end','Started')

replace the order

DATEDIFF('will_end','Started')

时光倒影 2024-08-23 11:27:37

我认为有3个参数需要传入
DATEDIFF ( datepart , startdate , enddate )

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

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

I think there are 3 parameter to be passed in
DATEDIFF ( datepart , startdate , enddate )

so your code would be
DATEDIFF ( dd , 'Started ','will_end' )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文