SQL DATEDIFF 不起作用?
我正在运行一个简单的 DATEDIFF 查询,但它似乎没有正确计算日期,或者我做错了什么。
如果我运行
PRINT DATEDIFF(Day, 2010-01-20, 2010-01-01)
RETURN 19
哪个是正确的。如果我将第一个日期的月份更改为二月(02),我会得到一些奇怪的东西。
PRINT DATEDIFF(Day, 2010-02-20, 2010-01-01)
RETURN 20
现在不应该是48之类的吗?
任何人都可以看到我做错了什么,或者如果我想要这些日期之间的天数,这不是要使用的正确函数吗?
我尝试过与另一位约会:
PRINT (2010-02-20) - (2010-01-01)
RETURN -20
非常感谢任何帮助。
谢谢 J。
I am running a simple DATEDIFF query but it doesn't seem to calculate the days properly or i'm doing something wrong.
If I run
PRINT DATEDIFF(Day, 2010-01-20, 2010-01-01)
RETURN 19
Which is correct. If i change the month in the first date to Feb (02) I get something strange.
PRINT DATEDIFF(Day, 2010-02-20, 2010-01-01)
RETURN 20
Now shouldn't it be 48 or something?
Can anyone see what i'm doing wrong or is this not the correct function to be using if I want the No of days between these dates?
I've tried taking one date from the other:
PRINT (2010-02-20) - (2010-01-01)
RETURN -20
Any help much appreciated.
Thanks
J.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你缺少引号
你得到 20 是因为
You are missing quotes
You're getting 20 because
如果您以这种方式运行它:
您将看到
哪些是结果或您放在此处的整数运算。
将日期常量括在单引号中:
If you run it this way:
you will see
which are results or the integer operations that you put here.
Enclose the date constants into single quotes:
如果你用撇号括住你的日期,它就会起作用 -
It works if you surround your dates with apostrophes -
这需要第二次约会 - 第一次约会。别忘了‘’。
That takes 2nd date - first date. Don't forget ' '.