What I want to do is to get the data of each "administrative" week (Monday to Sunday), that is if a month starts a Wednesday,the next week when I would launch the query, I would want the data from Wednesday, Thursday, Friday, Saturday, Sunday of the past week. This would get me data of week 1, and so forth for each week of the current month.
I made the following query:
with connection.cursor() as cursor:
sql = 'SELECT COUNT(*) ' \
'FROM panneau ' \
'WHERE year(datecreation) = year(now()) ' \
'AND month(datecreation) = month(now()) ' \
'AND datecreation [I don't know what to put here] '
cursor.execute(sql)
test = cursor.fetchall()
print(test)
But I don't know what to put in the last line.
In the end I should have 4 to 5 variables with the data of each "administrative week", which would take into account when the first week of the month starts and when the last week of the month ends.
Edit : for example with the month on July 2022
Week 1 = Friday 1st to Sunday 3rd.
Week 2 = the 4th to 10th.
Week 3 = 11th to 17th
Week 4 = 18th 24th
Week 5 = 25th to 31st
Some help would be helpful please, thank you.
To get the previous administrative week, you can check if a potential date exists in a range of prior week dates, offset by the current datetime's dow, generated by generate_series:
select p.datecreation from panneau p
where date(p.datecreation) in
(select date(p1.dt) from
(select now() - (extract(dow from now())::text || ' days')::interval - ((7-v)::text|| ' days')::interval dt
from generate_series(1,7) v) p1
where extract(month from p1.dt) = extract(month from now() - (extract(dow from now())::text || ' days')::interval))
See fiddle.
Related
I have a table with week number and day of week.
Example:
J2 = 18
G2 = Monday
How can I convert this to 2018-04-30?
I can find lots of threads for converting the other way around, meaning date to week. But I can't find anything on week + weekday to date.
Anyone know of a method?
The (optional) second argument of the WEEKDAY function determines what the first day of the week is. The two most common choices are:
1: Sunday (1) to Saturday (7)
or
2: Monday (1) to Sunday (7)
But, you can start on Wednesday, Friday, etc if you want. Pick an option now.
So, start with 1st January (of whichever unspecified year you're working with), and subtract the weekday of 1st January, according to whichever start-day you picked. This gives you a baseline for Week 1. To get to the start of Week n, you just need to add 7*(n-1) days.
Finally, you need to add the weekday back on. I'm going to recommend using MATCH on an array. Your array will be your weekdays, in order, surrounded by curly brackets. So, for Sunday-to-Saturday, you would use
MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
to get a number from 1 ("Sunday") to 7 ("Saturday"), or #NA if cell G2 does not contain a day-name.
Stick it all together, and you get something like this:
=DATE(YEAR(NOW()),1,1) - WEEKDAY(DATE(YEAR(NOW()),1,1),1) + 7*(J2-1) + MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
Try this, necessary comments in code:
Option Explicit
Sub GetDate()
Dim year As Date, day As Long
'convert day in G2 to integer
Select Case Range("G2").Value
Case "Monday"
day = 1
Case "Tuesday"
day = 2
'rest of cases
End Select
'declare what year you want
year = "01-01-2018"
'move from first week to the week specified in J2
year = DateAdd("d", (Range("J2").Value - 1) * 7, year)
'move back to first day of the week
Do While DatePart("w", year, vbMonday) <> 1
year = DateAdd("w", -1, year)
Loop
'find date of particular day
Do While DatePart("w", year, vbMonday) <> day
year = DateAdd("w", 1, year)
Loop
End Sub
The determination depends critically on your definition of "week number".
If you are using the ISO-8601 definition, where a Week starts on Monday and week 1 contains the first Thursday of the year, you can use this worksheet formula:
=DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)+$J$2*7+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1
you may need to change the commas to semicolons depending on your regional settings
To explain:
First, find the last Monday of the previous year:
DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)
Then add seven(7) times the number of desired weeks:
+$J$2*7
Then add the number of days from Monday to the desired day for that week:
+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1
I would like to get last week and next week number from week number.
How to get the next week number if it's December or the last week if it's January ?
I use ISO week numbers.
Desired results:
select datepart(year,dateadd(day,-1,week_dt_start)) * 100 +
datepart(week,dateadd(day,-1,week_dt_start)) as prev_week,
datepart(year,dateadd(day,1,week_dt_end)) * 100 +
datepart(week,dateadd(day,1,week_dt_end)) as next_week
from tablename
I need help writing one line of code in VBA to return the Month-Ending date. The logic for Month-Ending date works like this...
If the last day of the month ends on...
Sunday: Then last day of that month is that previous Saturday. (Yesterday)
Monday: Then last day of that month is that previous Saturday. (2 days ago)
Tuesday: Then last day of that month is that previous Saturday. (3 days ago)
Wednesday:Then last day of that month is the upcoming Saturday (3 days in the future)
Thursday: Then last day of that month is the upcoming Saturday (2 days in future)
Friday: Then last day of that month is the upcoming Saturday (1 day in future)
My current code is below. The formatting of the Month-Ending date is as follows. 2016-07-02
Sub Macro1()
With ActiveWorkbook.Connections("ABC Query").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"exec [dbo].[getBSC_Monthly] #MonthEndDate = **where I need the line of code**")
The Weekday() function will tell you what the current day of the week is (Sun=1, Mon=2, etc.). So, if Weekday() < 4, then you want the date Weekday() days ago. If WeekDay() >= 3, then you want 7 - Weekday() days in the future.
Function MonthEnd(d)
Dim actualmonthend, dow, ans
actualmonthend = DateSerial(Year(d), Month(d) + 1, 1) - 1
dow = Weekday(actualmonthend)
If (dow < 4) Then
ans = actualmonthend - dow
Else
ans = actualmonthend + (7 - dow)
End If
MonthEnd = ans
End Function
if you really just want an expression this would work:
DateSerial(Year(d), Month(d) + 1, 1) - 1 - Weekday(DateSerial(Year(d), Month(d) + 1, 1) - 1) + (7 * Abs(Weekday(DateSerial(Year(d), Month(d) + 1, 1) - 1) >= 4))
How to calculate the last day of the next month using vb.net?
try yo use DaysInMonth Function which returns the number of days in the required month.
Example
System.DateTime.DaysInMonth(2016,May)
This will return (31), so you can get the date format in addition to this number to get last day of this month.
An other way is to get the first day of the month after and substract 1 day.
Dim d As DateTime
d = DateTime.Now.AddMonths(2)
d = New DateTime(d.Year, d.Month, 1).AddDays(-1)
If you after the actual date....
You could use DateSerial(Year, Month, 0) this will return the last day of the month prior to the month entered.
Entering DateSerial(2016, 07, 0) will return "30/06/2016"
To get the last day of next month do DateSerial(Now.Year, Now.Month + 2, 0)
This works fine for February and also the year end as well (DateSerial(2016, 3, 0) returns "29/02/2016", DateSerial(2016, 11 + 2, 0) returns "31/12/2016")
Dim N = DateTime.Now.AddMonths(1)
Dim D = New DateTime(N.Year, N.Month, DateTime.DaysInMonth(N.Year, N.Month))
D will have the last day of next month.
I have a table with a bunch of dates (option maturity dates to be precise). I need to query this database to find the last day of a specific week that is stored in the table.
All I will be given to query this table is the year, the month and the specific week. And based on this I need to find the date that is stored in the table that matches this.
I've created the following query to find this specific date March 28 2013
SELECT M_SETNAME, M_LABEL, M_MAT FROM OM_MAT_DBF
WHERE M_SETNAME = 'IMM_OSET '
AND MONTH(M_MAT) = 3
AND YEAR(M_MAT) = 2013
AND ((DATEPART(day,M_MAT)-1)/7 + 1) = 5
Do you guys have any idea of how I can change the last condition so that March 28th will be considered the 5th week of the month and not the 4th week as it is currently doing.
You can also use DATEPART to get the number of the week (in the year), but then, you could also get the 1st of each month, and take the week too so you can have: WEEK OF MY DATE - WEEK OF FIRST DAY FOR THIS MONTH + 1.
Here you have an example...
DECLARE #Dt datetime
SELECT #Dt='03-28-2013'
SELECT DATEPART( wk, #Dt) - DATEPART( wk, Convert(Date,Convert(varchar(4),YEAR(#Dt))
+ '-' + Convert(varchar(2), MONTH(#Dt))
+ '-' + Convert(varchar(2), 1))) + 1
EDIT: Also, looking at your code, you could add the CEILING. If the result == 2.7, it means it passed the 2nd week, however, it gets rounded to 2 when it should actually be 3.
If you add the CEILING and the CONVERT to decimal should work..
SELECT MONTH(#Dt),
YEAR(#Dt),
((CEILING(Convert(decimal,DATEPART(day,#Dt)-1)/7)) + 1)