SQL 减去整整一年 [英] SQL Subtract exactly a year

查看:61
本文介绍了SQL 减去整整一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我要感谢所有帮助我的人,它给了我很多关于我应该如何做的想法,我想出了自己的方法我只需要帮助将其放入查询中

First I want to thank everyone for helping me, It gave me a lot of ideas on how I should do this and I came up with my own method I just need help putting it into a query

我希望用户输入日期,然后获取当前年份.减去两者,然后执行每个人都发布的 dateadd.这可能吗?最好的方法是什么?

I want the user to input a date, then get the current year. subtract the two and then do the dateadd that everyone was posting. Is this possible and what is the best way to do it?

year(getdate())-@DYYYY=Y 
dateadd(year,-Y,getdate())

推荐答案

根据您对硬编码年份值的评论,使用

Based on your comment regarding hard coded year values, the use of

 DATEDIFF(year,BOOKED,GETDATE())

获取自您所追求的日期以来的年数应该有望引导您朝着您所追求的方向前进.

to get the number of years since the date you're after should hopefully lead you in the direction you are after.

你可能会得到类似的结果:

You will probably end up with something like:

SELECT DATEADD(year, -DATEDIFF(year,BOOKED,GETDATE()), GETDATE())

<小时>

好的,看起来您真正想要做的(我可能错了,如果是这样,抱歉)就是按年份对预订进行分组.


Ok, it looks more like all you really want to do (I may be wrong, sorry if so) is to group the bookings by year.

以下结果是否有助于实现这一目标?

Will the result of the following help achieve that?

SELECT SDESCR,DATEADD(YEAR, DATEDIFF(YEAR, 0, BOOKED),0), Sum(APRICE) as Total, Sum(PARTY) as PAX
FROM  DataWarehouse.dbo.B01Bookings AS B101Bookings
GROUP BY SDESCR,DATEADD(YEAR, DATEDIFF(YEAR, 0, BOOKED),0)

正如我所说,这是对您目标的猜测,不一定是您问题的答案.

As I said, this is a guess as to your goal, not necessarily an answer to your question.

这篇关于SQL 减去整整一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆