SQL-Automatically Bucketize all dates into that weeks monday date- - sql

all- I'm looking to automatically standardize a certain date field into the monday date of that week. Essentially, if an entry came in with a date in this field of Tuesday, July 30th, 2013- I would want to standardize it to Monday, July 30th, 2013. I'd like to be able to apply this to only dates in a certain column where entries may have more than one date in seperate columns.
Thank you!!

If you want Monday of the current week you can use the following in SQL Server:
SELECT DateAdd (Day, 2 - DatePart (dw, YourDate), YourDate)
Depending on desired behavior, you may have to use SET DATEFIRST 2 to adjust the behavior.

List<List<myRecord>> recordList = new List<<myRecord>>();
List<DateTime> mondays = new List<DateTime>();
DateTime first = new DateTime(2013, 1, 30);
DateTime last = new DateTime(2013, 7, 29);
for (Date mon = first; mon <= last; mon = mon.AddDays(7)) {
recordList.Add(new List<myRecord>([SELECT * FROM myRecord WHERE myRecord.Date >= mon AND myRecord.Date < mon.AddDays(7)] ))
}
foreach (List<myRecord> monList : recordList) {
//do stuff with each monday bucket
}
dunno what technology youre using but maybe something like this would work?

Related

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

how to get date of 1st week of current month

I have to get/create date (date of first week) from the user input of day name
Examples-
if input saturday then date should be 7 (for current month 1st saturday)
if input sunday then date 1 (current month 1st sunday)
I am having to use lot of logic to get the date but couldn't get exact output
any suggestions on how to come up with the SQL query for such a function ?
If you are using SQL*plus in Oracle then the code will be as :
select next_day(sysdate,'&d')-7 from dual;
If any update required please do inform.
okay i have a link hereby where you can understand this and try to do
Your answer!!!
Try this.
SELECT DATEPART(dw,DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0))
input: if getdate() returns date in jan 2017
output: 1
input:if getdate() returns date in jan 2017
output: 4

SQL Query Return All Date which is weekend from a table

I have a table which has a field called Date , contains a list of date, and now I want to return a table contains a list of Date which is weekend with SQL Query.
I am using Microsoft Access and so far I have tried DATEPART and WEEKDAY, but they can only check if a specified date is weekend or not, not a list of Date.
How can I do that? Thank You
This seems to explain what you need...
http://www.techonthenet.com/access/functions/date/datepart.php
DATEPART("w" will give the weekday from 1 to 7.
vbMonday will ensure that Monday is weekday 1.
=>
Saturdays are weekday 6 and Sundays are weekday 7.
WHERE
DATEPART("w", yourField, vbMonday) >= 6

Get birthday greater than current date

my user table is as follows:
birhtMonth: int
dayOfBirthday: int
HireDate: Date
REQUIREMENT: i want to get all upcoming birthdays and hire dates (day/month year is excluded here) in next 6 months, putting in consideration that for current month the day should be greater than today, so here's what i did:
#Query("from User u where ( (u.birthMonth in (8,9,10,11,12,1)) or (month(u.hireDate) in (8,9,10,11,12,1)) ) and u.company = :company")
this gets all upcoming birthdays & hire dates in next six months but it gets birthdays & hire dates in this month for days before & after current day, and it should only get results > current day and ignore results < current day in this month.
EXAMPLE:
today's date is 8/3/2013, if there's birthday with birthMonth=8 and dayOfBirth=3 or 2 or 1 it should be ignored only dayOfBirth > 3 in current month is considered, also if there's hirDate like:
2011-08-01
2012-08-02
2012-08-03
they should be ignored too, please advise how to solve this in sql or hql.
Your hireDate is of type Date, so use a date comparison and use between such as:
(hireDate between :toDayParam and :sixMonthLaterParam)
for birthDate, you can compare with lpad(birthMonth, 2, 0) + lpad(birthDate, 2, 0) but you shall care about whether six month later is in next year or current year.
I think your looking for the DATE functions. They can greatly help you out here. Specifically the DATEADD function. Take a look at the code I made here.
SELECT * FROM dbo.product
WHERE dbo.product.stockDate > GETDATE()
AND dbo.product.stockDate < GETDATE() + DATEADD(month, 6, dbo.product.stockDate)
AND dbo.product.expirationDate > GETDATE()
AND dbo.product.expirationDate < GETDATE() + DATEADD(month, 6, dbo.product.expirationDate)
This will guarantee that the stockDate and the expirationDate are greater than the current date and less than the current date + 6 mo. DATEADD works as follows DATEADD(-what you want to increment by-, -how much you want to increment-, -date to add to-).

Get 1st Day of Week (Sunday)

I have this formula in my column to calculate the start date of a given week :
dateadd(week,[Week]-(1),
dateadd(day,(-1),
dateadd(week,
datediff(week,(0),
CONVERT([varchar](4),[Year],(0))+'-01-01'),
(1))))
Where Week and Year are other fields like 38 and 2012
Problem is, it calculates the start date of week 38/2012 as a monday (17th Sept), I would like it to be a sunday instead (16th Sept) is this possible?
Many thanks.
This will return you the first day of the week, given a week number and a year, assuming that the first day of the week is a Sunday.
Standard exclusions apply, e.g. don't try year 1499.
declare #tbl table ([Week] int, [Year] int)
insert #tbl select 38,2012
union all select 1,2012
union all select 0,2012
union all select 1,2013
select DATEADD(
Week,
[Week]-1,
DATEADD(
Day,
(8-##datefirst)-DATEPART(dw, CAST([Year]*10000+101 AS VARCHAR(8))),
CAST([Year]*10000+101 AS VARCHAR(8))))
from #TBL
Result
2012-09-16 00:00:00.000
2012-01-01 00:00:00.000
2011-12-25 00:00:00.000
2012-12-30 00:00:00.000
Note that the Week number starts from 1, and if the week doesn't start on a Sunday, then the first day of that week could end up in an earlier year (row #4). Because the Weeks are relative, you can use Week 0, -1 etc and it will still give you a result (row #3), rightly or wrongly.
You may also notice I used a different method to create a date out of the year, just as an alternative.
The (8-##datefirst) portion of the query makes it robust regardless of your DATEFIRST setting.
If you want the first day of the week to be Sunday you could change your database to make it so, using the DATEFIRST setting.
function getFirstDateOfThisWeek(d)
{
var TempDate = new Date(d || new Date());
TempDate.setDate(TempDate.getDate() + (#Config.WeekStartOn - 1 - TempDate.getDay() - 7) % 7);
return TempDate;
}
var StartDate = getFirstDateOfThisWeek(new Date()); //1st date of this week