SQL Subtract exactly a year

前端 未结 6 1453
独厮守ぢ
独厮守ぢ 2020-12-06 17:24

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

相关标签:
6条回答
  • 2020-12-06 17:57
    select DATEADD(yy, -1, getdate())
    
    0 讨论(0)
  • 2020-12-06 18:03

    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?

    0 讨论(0)
  • 2020-12-06 18:04

    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.

    0 讨论(0)
  • 2020-12-06 18:08

    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.

    0 讨论(0)
  • 2020-12-06 18:23

    Use this:

    dateadd(year, -1, getdate())
    
    0 讨论(0)
  • 2020-12-06 18:24

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

    SELECT GETDATE() - 365
    
    0 讨论(0)
提交回复
热议问题