tips54
asked on
capture data for the week
Hello experts,
I need to help with the following syntax. I need to capture sales for the current and sales for the same week last year.
Current calender week: ?
Current calender week Last year: ?
I need to help with the following syntax. I need to capture sales for the current and sales for the same week last year.
Current calender week: ?
Current calender week Last year: ?
Here is full solution:
DECLARE @datecol datetime = GETDATE();
DECLARE @WeekNum INT
, @YearNum char(4);
SELECT @WeekNum = DATEPART(WK, @datecol)
, @YearNum = CAST(DATEPART(YY, @datecol) AS CHAR(4));
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
--
Declare @NextDate datetime= DATEADD(YEAR,-1, getdate())
SET @YearNum= CAST(DATEPART(YY, @NextDate) AS CHAR(4));
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
DECLARE @datecol datetime = GETDATE();
DECLARE @WeekNum INT
, @YearNum char(4);
SELECT @WeekNum = DATEPART(WK, @datecol)
, @YearNum = CAST(DATEPART(YY, @datecol) AS CHAR(4));
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
--
Declare @NextDate datetime= DATEADD(YEAR,-1, getdate())
SET @YearNum= CAST(DATEPART(YY, @NextDate) AS CHAR(4));
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
ASKER
Hi Ravi,
Your code returns the current week number for the year. (10)
what I'm after is something that returns : 3/2/2014 - 3/8/2014
And 3/2/2013 - 3/8/2013
Your code returns the current week number for the year. (10)
what I'm after is something that returns : 3/2/2014 - 3/8/2014
And 3/2/2013 - 3/8/2013
3/2/2014 - 3/8/2014 is the date range of current year of the 10 th week.
and date of 10th week of last year was 3/2/2013 - 3/8/2013
and date of 10th week of last year was 3/2/2013 - 3/8/2013
ASKER
If I have a table with multiple invoices with dates, how would the syntax look for sum of invoices for just week 10?
DECLARE @FirstDate as datetime = DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) --AS StartOfWeek;
DECLARE @SecondDate as datetime = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) --AS EndOfWeek;
Select sum(Amount) from Invoices Where InvDate > = @FirstDate and InvDate <=@SecondDate
DECLARE @SecondDate as datetime = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) --AS EndOfWeek;
Select sum(Amount) from Invoices Where InvDate > = @FirstDate and InvDate <=@SecondDate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
where does it differentiate Current and Last year?
I get this error when I run the code:
Cannot assign a default value to a local variable.
I get this error when I run the code:
Cannot assign a default value to a local variable.
ASKER
Thanks ravikantninave,
I got it. I had to set those values on the next line. This might be a Sql 2005 issue.
Thanks again.
What I Used:
/*
DECLARE @datecol datetime
set @datecol = GETDATE();
DECLARE @WeekNum INT
set @WeekNum = DATEPART(week,GETDATE());
DECLARE @YearNum char(4);
set @YearNum =DATEPART(year,GETDATE());
Declare @NextDate datetime
SET @NextDate= DATEADD(YEAR,-1, getdate())
DECLARE @YearNum2 char(4);
SET @YearNum2= CAST(DATEPART(YY, @NextDate) AS CHAR(4));
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum2) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum2) + (@WeekNum-1), 5) AS EndOfWeek;
*/
I got it. I had to set those values on the next line. This might be a Sql 2005 issue.
Thanks again.
What I Used:
/*
DECLARE @datecol datetime
set @datecol = GETDATE();
DECLARE @WeekNum INT
set @WeekNum = DATEPART(week,GETDATE());
DECLARE @YearNum char(4);
set @YearNum =DATEPART(year,GETDATE());
Declare @NextDate datetime
SET @NextDate= DATEADD(YEAR,-1, getdate())
DECLARE @YearNum2 char(4);
SET @YearNum2= CAST(DATEPART(YY, @NextDate) AS CHAR(4));
-- once you have the @WeekNum and @YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum2) + (@WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum2) + (@WeekNum-1), 5) AS EndOfWeek;
*/
ASKER
How would I get the sum for the quarter?
ASKER
I would like to capture the same data quarterly compare to last years quarter.
first three months of the year? or what? Find quarter :
SELECT DATEADD(mm, (quarter - 1) * 3, year_date) StartDate,
DATEADD(dd, -1, DATEADD(mm, quarter * 3, year_date)) EndDate,
quarter QuarterNo
FROM
(
SELECT '2013-01-01' year_date
) s CROSS JOIN
(
SELECT 1 quarter UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) q
SELECT DATEADD(mm, (quarter - 1) * 3, year_date) StartDate,
DATEADD(dd, -1, DATEADD(mm, quarter * 3, year_date)) EndDate,
quarter QuarterNo
FROM
(
SELECT '2013-01-01' year_date
) s CROSS JOIN
(
SELECT 1 quarter UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) q
ASKER
Thank you.
the second question was answered here:
https://www.experts-exchange.com/questions/28381957/capture-data-for-each-quarter.html
the second question was answered here:
https://www.experts-exchange.com/questions/28381957/capture-data-for-each-quarter.html
select DATEPART(WEEK,GETDATE())