SQL converting number of days to weeks - sql

I have a SQL table with a column that says number of days, and contains entries like 23, 26, 45, etc...
I am trying to convert each entry to a "week number". In essence, what I mean is that if my day entry is between 0 and 6, then, this is Week 1, if it is 7 and 13, then this is Week 2, 14 and 20, week 3, etc... Is there an "efficient" way to do this in SQL?
Thanks.
Thomas.

You need just the standard divide function. It ignores the remainder:
SELECT (Days / 7) + 1

You can try this and no need to add +1;
SELECT (Days / 7.00)

Related

Query to return records between business years not working correctly

I have found myself in problems querying for records beween business years.
In this case, business year always starts on 01/08/YYYY and end on 31/07/YYYY.
For example:
For 2019-2020 business start: 01/08/2019 ends 31/07/2020
For 2020-2021 business start: 01/08/2020 ends 31/07/2021
When I opened the database today, records still show up even though it is a new business year.
Its as though my queries do not work from the beginning of a new Business year. I have:
'To return records for the Current Business year:
SELECT * FROM tblData WHERE (((DateDiff('m',[PaymentDate],DateSerial(Year(Date()),1,1))) Between -6 And 5));
'To return records for Previous Business year:
SELECT * FROM tblData WHERE (((DateDiff('m',[PaymentDate],DateSerial(Year(Date())-1,1,1))) Between -6 And 5));
My question is, how do I correctly query records for the Current business year and Previous business year?
For the previous business year:
select t.*
from tblData t
where year(dateadd('m', -8, PaymentDate)) = year(date()) - 1
The logic is to subtract 8 months from the date and then extract the year.
If the start and end of business year is constant (yyyy/08/01 - yyyy+1/07/31) then try this - for the current year:
SELECT t.*
FROM tblData As t
WHERE t.PaymentDate BETWEEN DateSerial(Year(Date()), 8, 1) AND DateSerial(Year(Date())+1, 7, 31);
for previous year:
SELECT t.*
FROM tblData As t
WHERE t.PaymentDate BETWEEN DateSerial(Year(Date())-1, 8, 1) AND DateSerial(Year(Date()), 7, 31);
If you would like to pass year value to the query, try this:
--pass year to the query to grab data for business year yyyy/08/01 - yyyy+1/07-31
PARAMETERS BusinessYear SHORT;
SELECT t.*
FROM tblData As t
WHERE t.PaymentDate BETWEEN DateSerial(Year(BusinessYear), 8, 1) AND DateSerial(Year(BusinessYear)+1, 7, 31);
For further details, please see: PARAMETERS declaration (Microsoft Access SQL)

DateAdd - SQL expression

I am trying to create an expression with SQL. The expression value is trying to assign a value to the days of the week. Sunday is 1 through to Saturday which is 7. I am then trying to add 5 days if it is a Monday so that it lands on a Friday otherwise every other day add 7 days. E.g.
WeekDay Inlist("2",",") DateAdd(5, "d") OR ds WeekDay Inlist("1,3,4,5,6,7",",") DateAdd(7, "d")
However this does not seem to work?
So basically I enter my date in a databox as 15/03/2017 which is a Wednesday,
Our expression would say if its a Monday to add 5 days and therefore result in false. Because its a Wednesday we would need to say add 7 days, this ignores Saturday and Sunday and therefore would result in True. This would land on the following Wednesday.
Any ideas? :D
Not sure which variation of SQL you're using, but there's likely an equivalent to the T-SQL CHOOSE() and DATEPART() functions in most variants that will do this.
The expression syntax could end up something like this:
DATEADD(CHOOSE(DATEPART("dw", date), 7, 5, 7, 7, 7, 7, 7), "d", date)

SSRS parameter default to most recent 1st April

I have a requirement for an SSRS parameter that will be for a start date of the most 1st April, so today it will report from 1/4/2015 with 10 weeks data, but after the 1/4/16 it will default to 1/4/16.
Can anyone help with this please?
I can find most recent month, or current month etc, but this one is a little tricky. Thankyou.
I am using SQL Server 2012 and Visual Studio 2010
You could use the following expression:
=IIF(Today() >= DateSerial(Year(Today()), 4, 1), DateSerial(Year(Today()), 4, 1), DateSerial(Year(Today()) - 1, 4, 1))
The idea is to check if the 1st of April of the current year has been reached (Today() >= DateSerial(Year(Today()), 4, 1)).
If yes, take the 1st of April of the current year: DateSerial(Year(Today()), 4, 1)
If no, take the 1st of April of the previous year: DateSerial(Year(Today()) - 1, 4, 1)

Excel year-week combination calculation

I am currently making an excel model where the date is an important aspect of it. However as the information comes from a different source it is sometimes structured in such a way that it is not realistic.
I have a problem with the calculation of the year and week combination when adding weeks or subtracting them.
The start data looks like this:
yyyyww (example: 201525, year 2015 week 25)
Now if I want to add for example 3 weeks, I can just do that by adding 3, results is 201528. However when it comes to 201552 (and sometimes yyyy53) it is more difficult as I need to calculate to 2016.
The same goes for when calculating back in the time, towards 2014 or lower. Does anyone know or have a solution for this? Maybe in VBA? or a formula trick?
a formula to add 1 to your week number format looks like this:
=YEAR(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7) * 100 +
WEEKNUM(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7)
it will create a sequence of week numbers that includes: 201452 => 201453 => 201502
LEFT(A2, 4) and MID(A2, 5, 2) extract the year and week components
date(year, 1, 1) + (week - 1) * 7 converts that to an actual Excel Date value, e.g. 201502 => 2015-01-08
date(year, 1, 1) + week * 7 will create a date that is 1 week after the "current" date
year(date) * 100 + weeknum(date) converts that date back to yyyyww format

Rolling 6 Months

I have 2 parameters in my report, Month and Year.
I also have a table which in one column has the count of rows for that particular month and another column which has the count of rows for that specific month and the next 5 months (Rolling 6 month).
Both of these columns have an expression which links to the parameters as shown below:
Month Expression:
6 Month Expression:
My parameters use specific values which i have put in (1 - 12) so when my rolling month adds 1 value each time as you can probably guess this wont work when I come near the latter months of the year as my expression does not roll over to next year and just stops at December.
Overview: Parameters are integer values 1 - 12 and I am not sure how to calculate a rolling 6 months which will carry over to the next year when selecting months late in the year. e.g. If I select November as my Month parameter, my rolling 6 months will only display the sum of rows for November and December, not going to the next year. I am assuming this is because my month values are integers and in my expression I add numbers to the integers for each month therefore my rolling 6 will be trying to add months 11, 12, 13, 14, 15, 16 when obviously months only go to 12.
This is not an answer to your question, but looking at this, you may be able to find a solution with SQL itself.
See the query below. Consider this as your report source sql.
DECLARE #A TABLE (ID INT IDENTITY(1,1),DT DATE)
INSERT INTO #A (DT)
VALUES
('2013-01-26'),('2013-02-23'),('2013-03-20'),
('2013-04-23'),('2013-05-23'),('2013-07-23'),
('2013-08-23'),('2013-08-29'),('2013-09-23'),
('2013-12-10'),('2014-03-01')
If you join the result with itself, some what like below, you will get the result in sql query itself, you need to only show it in report. See below.
SELECT DATEPART(YEAR,DT) [Y],DATEPART(MONTH,DT) [M],COUNT(*) [ROLLING 6]
FROM (
SELECT A.*
FROM #A A,#A B
WHERE ((DATEPART(YEAR,B.DT) * 12) + DATEPART(MONTH,B.DT)) BETWEEN
(DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) AND
((DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) + 6)) LU
GROUP BY DATEPART(YEAR,DT),DATEPART(MONTH,DT)
ORDER BY [Y],[M]