SQL Query Return All Date which is weekend from a table - sql

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

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...

SQL Server: Count number of records on weekly basis (Week = Thursday to Wednesday)

I need some help in writing an SQL in SQL Server where I need to count number of rows group by weeks. There is a tricky description of week which is following
- For any date before 08/13/2015 the week is of 7 days (i.e. from Thu through Wed)
- For date 08/13/2015 the week is consider a 9 day week (i.e. from Thursday through Friday so its between 08/13/2015 through 08/21/2015)
- For date 08/22/2015 the week is back to 7 days (i.e. Sat through Friday)
Now having said all the above the result I want to see in my report is the following way . NOTE: WE column in the below attached image is the last day of the week for the range.
Sample Result Image
Just write a case statement for the 3 different options. You can find the start day with something like this:
DATEADD(week, DATEDIFF(day, 3,getdate()) / 7, 3) -- Thursdays
DATEADD(week, DATEDIFF(day, 5,getdate()) / 7, 5) -- Saturdays
The numbers 3 and 5 come from the fact that day 0 (=1.1.1900) is Monday.
If you use this a lot, it might be a good idea to write a inline table valued function to return the dates you need.

Create date of a specific weekday two weeks before the current week

I'm trying to create a SQL statement (in H2 Dialect) that creates a DATE which is
the tuesday in the "week-of-year" that is two weeks from the current week.
For example, given CURRENT_DATE() returns 2014-04-23, the date that I want would be 2014-04-08, which is the Tuesday in the week that is two weeks before the week the Date "2014-04-23" is in.
How can I express this with H2's Date and Time functions?
In sql-server I would do it like this
DECLARE #date DATE = GETDATE();
SELECT DATEADD(DAY,3-DATEPART(WEEKDAY,#date),DATEADD(WEEK,-2,#date))
Where '3' represents day of the week (Tuesday) and '-2' represents subtracting two weeks.
So now in h2 it is very similar
SELECT DATEADD('DAY',3-DAY_OF_WEEK(CURRENT_DATE()),DATEADD('WEEK',-2,CURRENT_DATE()))

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

Select dates within the last week

I want to perform an operation in crystal report.
I have a db table contains a date column.
I want to filter and get the rows having data created in last week(last sunday to last saturday = 7 days).For example if today is 24th August Wednesday, then I need data from 14th August(Sunday) to 20th August(Saturday).
Basically I want to find 2 dates and filter the date column.
Date1 = Date(CurrentDate)-Day(7 + WeekDayinNum(CurrentDate)) ; (Ex:for my example it will be 10)
Date2 = Date(CurrentDate)-Day(WeekDayinNum(CurrentDate))
I do not know the Date APIs properly,can anybody help me in this.
This is a common enough date range that CR provides it for you. In your record selection formula, you can just add {table.date} in LastFullWeek
From CR, "LastFullWeek specifies a range of Date values that includes all dates from Sunday to Saturday of the previous week."
Add this to the record selection formula:
{table.date_field} IN Last7Days
If today is Sunday(1) you want rows that are between 7 and 1 days old,
If today is Monday(2) you want rows that are between 8 and 2 days old,
If today is Tuesday(3) you want rows that are between 9 and 3 days old,
etc.
SELECT *
FROM `tablename`
WHERE `somedatefield` >= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW()) + 6) DAY)
AND `somedatefield` <= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW())) DAY)