如何获取某一个月和某一天的上一年的日期...反之亦然当年

时间:2015-06-26 09:21:17

标签: sql-server date

我想知道是否有人可以帮助我解决我的困境,我有一块sql,它必须在当前时间线返回一定数量,开始日期必须是2014-03-01和结束日期2015-03-01。现在棘手的部分是它应该每年自动更改所以我不必更改它,例如明年应该是2015-03-012016-03-01

我写过的SQL:

 SELECT c.Name AS Biller,SUM(TotalTransactions) AS TotalTransactions, 
  SUM(TotalPay@Fee)/1.14 AS TotalPay@Fee
 FROM tblPay@DailyRetailerBillerTotals d 
 LEFT OUTER JOIN tblPay@company c (nolock) ON d.ClientID = c.RecID 
 WHERE Name = 'Name of Biller'
 AND DateCreated >= 'start date' --2014-03-01
 AND DateCreated <  'end date' -- 2015-03-01
 GROUP BY c.Name
 ORDER BY c.Name Asc

4 个答案:

答案 0 :(得分:0)

This will get you the previous year to when the query is run.

datepart(Year,dateadd(YEAR,-1,GetDate()))

This should do what you need.

dateadd(YEAR,-1,'2015-03-03')

Just replace '2015-03-03' with your date column, and that will give you your start date.

Here's how it looks in your query

SELECT c.Name AS Biller,SUM(TotalTransactions) AS TotalTransactions, 
  SUM(TotalPay@Fee)/1.14 AS TotalPay@Fee
 FROM tblPay@DailyRetailerBillerTotals d 
 LEFT OUTER JOIN tblPay@company c (nolock) ON d.ClientID = c.RecID 
 WHERE Name = 'Name of Biller'
 AND  dateadd(YEAR,-1,DateCreated) >= 'start date' --2014-03-01
 AND DateCreated <  'end date' -- 2015-03-01
 GROUP BY c.Name
 ORDER BY c.Name Asc

答案 1 :(得分:0)

it's not that pretty but it should work:

  SELECT c.Name AS Biller,SUM(TotalTransactions) AS TotalTransactions, 
  SUM(TotalPay@Fee)/1.14 AS TotalPay@Fee
 FROM tblPay@DailyRetailerBillerTotals d 
 LEFT OUTER JOIN tblPay@company c (nolock) ON d.ClientID = c.RecID 
 WHERE Name = 'Name of Biller'
 AND DateCreated >= CAST(CAST(YEAR(GETDATE()-1) AS VARCHAR(4)) + '-03-01' AS DATE)
 AND DateCreated <  CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '-03-01' AS DATE)
 GROUP BY c.Name
 ORDER BY c.Name Asc

EDIT: datatype DATEworks only for SQL Server 2008 or later, if you're running a previous version use DATETIME instead.

答案 2 :(得分:0)

You can use the below query to get Start and End Date values in the variable and then use the values in your query:

DECLARE @year INT = Datepart(YEAR, getdate())
DECLARE @month INT = 3
DECLARE @day INT = 01
DECLARE @EndDate datetime
DECLARE @StartDate datetime

SELECT @StartDate = CAST(CONVERT(VARCHAR, @year - 1 ) + '-' + CONVERT(VARCHAR, @month) + '-' + CONVERT(VARCHAR, @day) AS DATETIME)
SELECT @endDate = CAST(CONVERT(VARCHAR, @year) + '-' + CONVERT(VARCHAR, @month) + '-' + CONVERT(VARCHAR, @day) AS DATETIME)

select @StartDate as [StartDate], @endDate as [EndDate]

答案 3 :(得分:0)

The following should give you the dates you need:

SELECT  YearStart = CAST(CAST(
                            DATEPART(YEAR, GETDATE()) - 
                                CASE WHEN DATEPART(MONTH, GETDATE()) < 3 
                                    THEN -1 
                                    ELSE 0 
                                END AS VARCHAR(4)) + '0301' AS DATE),
        YearEnd = CAST(CAST(
                            DATEPART(YEAR, GETDATE()) - 
                                CASE WHEN DATEPART(MONTH, GETDATE()) < 3 
                                    THEN 0 
                                    ELSE 1 
                                END AS VARCHAR(4)) + '0301' AS DATE);

The principle is to get a year, then add '0301' to it (1st March) to get your date. Then you just need to apply some logic to get the correct year, if the date is January of February, then you need to deduct one from the year to get the previous 1st March.