Week Start date from a Date MS Access - ms-access

I have a field with dates, I will like to run a query that updates another field with the Week Start Date (Sunday). How can I do that ?
I am using MS Access.

You should be able to do that with a combination of the DateAdd() and Weekday() functions, as in:
UPDATE theTable SET weekStartDate = DateAdd("d", -Weekday(theDate) + 1, theDate)

Related

SSRS - Set param to be the time right now but on a different date

I was asked to create an SSRS Report, but taking the live sales of today and compare it to sales the exact same time as a date to be named later. For example, "How are we doing compared to this time on last Saturday?" The user would simply pick the date, and the query would calculate sales for both Today() and #BusinessDate, from the start of the business until Today()'s time.
I am sure I can do the query, but I don't know how to set a query parameter for this.
Please note, Today() and Now() throw errors, but GETDATE() works fine.
Got it:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), #BusinessDate, 112) + ' ' + CONVERT(CHAR(8), GETDATE(), 108))

Have a table that includes startdate and durationminutes. Trying to get that to give new end date in SQL

Title says it all.
Table looks like this
Table: Time
First Column: startdate (shown as yyyy-mm-dd hh:mm:ss)
Second COlumn: durationminute (shown in minutes)
Since the above doesn't give me an end date, I'm trying to create a query which will do that.
I'm assuming this is going to be a dateadd function but I can't get it to work. The durationminutes is a variable to is dependent on that one row so date_add(minutes, 30, startdate) wouldn't work as there is no constant.
I've tried
date_add(days, durationminutes/1440, startdate)
But am getting a syntax error.
MySQL understands date arithmetics. Assuming that startdate is of date or datetime datatype (as it should be!), you can do:
select t.*,
startdate + interval durationminute minute as enddate
from time t

access query that returns records for previous year up to same day, same month as today but for previous year

I need help specifying an access criteria on a date field that would pull records from my database from the beginning of last year, 1/1/2014 to a date that has the same day, and same month as today. The reason for this information is to be able to able to compare year-to-date records(and later counts) for this year to year-to-date's count for last year... thus, if today's date is 8/20/2015, I would want to be able to pull from 1/1/2015 to 8/20/2015 and then compare it to 1/1/2014 to 8/20/2014.
Just for heads-up, I am using the same query and form to count records based on weekly, quarterly date-ranges, and so I cannot use textboxes with "Start" and "End" dates. Also, I cannot pre-specify any date in my query. Any idea will be greatly appreciated. Thank you all.
To get last year's year-to-date DateSerial will do what you want.
Where [DateColumn] >= DateSerial(year(now)-1,1,1)
and [DateColumn] <= DateSerial(year(now)-1,month(now),day(now))
Another option
Where [DateColumn] >= dateadd("yyyy", datediff("yyyy", 0, now)-2, 2 )
and [DateColumn <= DateAdd("yyyy",-1, now)
You need to use Date() in SQL:
Where [DateColumn] >= DateSerial(Year(Date())-1,1,1)
And [DateColumn] <= DateAdd("yyyy",-1,Date())
The following expression can be used as the criteria for the date field in the query designer
>="01/01/" & (Year(Date())-1) AND <=Day(Date()) & "/" & Month(Date()) & "/" & Year(Date())-1
Warning: using strings to build dates should be avoided when possible. DateSerial() is a better approach, but this will work in MS Access (Jet/ACE).

SSRS Expression - Find First Day of Week From Week value

I've got an SSRS report that returns unique login count to our VDI Pools broken down by week.
For example for January it would read:
Week # of Logins
2015_JANUARY_WEEK_NO_1 3
2015_JANUARY_WEEK_NO_2 49
I'm using the Week column to link to another report that'll display the selected week's logins so I have to get the first day of the selected week and the last day of the select week and pass them to the other report as parameters.
I know how to get pull the year and week # from the Week column.
=Mid(Fields!Week.Value,1,4)
gives me the year and
=Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
gives me the week.
I found this T-SQL that works:
DECLARE #WeekNum INT
, #YearNum char(4);
SELECT #WeekNum = 2
, #YearNum = 2015
-- 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;
but I cannot figure out how to turn that into an expression that doesn't throw an error.
This is what I've got so far:
=DateAdd("w", DateDiff("w", 6, '1/1/' + (Mid(Fields!Week.Value,1,4))) + (Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
- 1), 6)
and when I try to run the report in design view it returns an Expression expected error.
Edit
Sorry, I guess I should've posted my original query that's populating the report. Here it is below:
SELECT Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1) as 'Week'
, Count(DISTINCT SUBSTRING(ModuleAndEventText,LEN('User ') + 2
, CHARINDEX(' requested', ModuleAndEventText) - LEN('User ') - 2)) as WeekCount
FROM VE1_UserLogins
WHERE DesktopId = #Pool
AND ([Time] BETWEEN (#StartDate) and (DATEADD(ms, -1, #EndDate +1)))
GROUP BY Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1),YEAR(Time),MONTH(Time)
ORDER BY YEAR(Time), MONTH(Time), Week
I am assuming your Fields!Week.Value is string in date format like MM/dd/yyyy or something similar so if you want to get the start of that week Sunday as first day and Saturday as last day and you want to get there date then you should use below expression,
For start of week,
=DateAdd("d",1- DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
For end of week,
=DateAdd("d", 7 - DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
----------
UPDATE
Now there are two ways to achieve what you want
1)Calculate start of week and end of week at the sql and get the result to directly display on repott
2) Get time field from the sql and set expression in the SSRS
For the first way to achieve you need to add these two lines in your select sql statement
DATEADD(dd, -(DATEPART(dw, MIN(Time))-1),MIN(Time)) AS 'StartOfWeek'
,DATEADD(dd, 7-(DATEPART(dw, MIN(Time))), MIN(Time)) AS 'EndOfWeek'
Second way to achieve is
Update your Sql statement to get the date part and then include that date into the above given expression.
So for you your query you need to add,
,MIN(Time) AS WeekDate
Then you can use the above expression with using the incoming field WeekDate as input(I have updated the expression).
But, If you have no actual need for the date other than calculating the start and end of week then use the first method to get the data from the sql server as formatted.

mysql select all from today up until now

I'm trying to build a query where it will return all rows from today's date, but only up to this point in time of the day.
E.G.
If it's 12.30pm, I'd like it to return all rows with the date from 00:00 to 12:30 on today's date, but nothing after 12:30, or whatever time is when the scrips is run
DATE(expression) removes the time and NOW() includes the current time:
SELECT * FROM tbl WHERE iDate > DATE(NOW()) and iDate < NOW()
have you tried:
SELECT * FROM table WHERE date < TODAY && date > DAYBEFORE
something like this should work fine for timestamp saved dates. Not sure about DATE formated values
You can specify a starting timestamp and an ending timestamp:
where created >= timestamp_start && created < timestamp_end
Note that my answer assumes you're using some server side programming language to run these sql queries, and thus you would be passing a variable into timestamp_start and timestamp_end. time format: use Unix Timestamp