开发者

SQL Subtract exactly a year

开发者 https://www.devze.com 2023-01-26 21:48 出处:网络
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

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

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 po开发者_Python百科ssible and what is the best way to do it?

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


Use this:

dateadd(year, -1, 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.


To subtract a year from a date simply use DATEADD() function

SELECT DATEADD(year, -1, GETDATE())

Edited:

SELECT SDESCR,DYYYY, Sum(APRICE) as Total, Sum(PARTY) as PAX
FROM  DataWarehouse.dbo.B01Bookings AS B101Bookings
WHERE 
(BOOKED <= Convert(int, Convert(datetime, Convert(varchar, DatePart(month, GETDATE())) + '/' + Convert(varchar, DatePart(day, GetDate())) + '/' + DYYYY))
Group By SDESCR,DYYYY
Order by DYYYY

Edited 2:

I just ran this statement

select  Convert(datetime, Convert(varchar, DatePart(month, GETDATE())) + '/' + Convert(varchar, DatePart(day, GetDate())) + '/' + '2007')

Which runs fine. so my question is what is stored in the DYYYY Column? Does it always contain a valid year, can it contain Nulls?


OK... From your response to Joe Stefanelli, I think that part of what you are really trying to do is avoid having to rewrite the query each year. If that is the case, then you can create a numbers table. A simple and fast example would be like this...

 SELECT TOP 3000
        IDENTITY(INT,1,1) as N
   INTO #Numbers
   FROM Master.dbo.SysColumns sc1,
        Master.dbo.SysColumns sc2

Instant table with integers from 1 to 3000.

This allows you to join or subselect against the #Numbers table for your query. If you want to make a more limited range, then you can make a table with just the years you like (or a table valued function that will make the same table dynamically).

You could also take it a bit further and implement a table valued function that will return a two column result

year offset
2010 0    --Produced from DATEPART(y,GETDATE())
2009 -1    --loop/set subtract 1
2008 -2    --repeat until you have enough...

This way, your where clause could read something like

SELECT *
FROM yourTable, yourFunction
WHERE ((DYYYY = CAST(yourFunction.year as VARCHAR) AND (BOOKED <= DATEADD(yy, yourFunction.offset, GETDATE()))

Note that while the tv functions should save you a bit of maintenance programming each year, you may suffer some minor performance hits.


select DATEADD(yy, -1, getdate())


The simplest way to get the date 1 year ago is:

SELECT GETDATE() - 365
0

精彩评论

暂无评论...
验证码 换一张
取 消