Sorting by month in MS Access - ms-access

I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.

If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.

This should work
SELECT *
FROM TableName
OrderBy Month(date_field)

I would store the month as an integer 1-12 then you can easily sort them.

I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.

Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.

I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.

Related

Database structure for storing schedules/cron job?

I am stuck with a problem. In an app's db, I am having a schedule table which will store user provided schedules. E.g
Daily
Every Week
Twice a Week
Every 3rd (or any user chosen) day of week
Every Month
Twice a month
Every x day of month
Every x month of year
And so on. These schedules will then provide reference point to schedule different tasks or identify their repeat-ance.
I am not able to think of a proper database structure for it. The best I can get is to have a table with following columns:
Day
Week
Month
Year
type
Then store the specified schedule in the related column and provide the type.
e.g Every week can go like 1 in week column and 1 (designated value for repeating whole) or something like that.
The problem with this approach is that this table is gonna be used very frequently and the data retrieved will not be straightforward. It will need calculation to know the schedule type and hence will require complex db queries to get each type of schedule.
I am implementing it in Laravel app if that can provide any other methodology. It's a SAAS app with huge amount of data related to the schedule table.
Any help will be very much appreciated. Thanks
I suggest you are approaching the problem backwards.
Devise several rules. Code the rules in your app, not in SQL. When inserting an event, pre-fill a calendar through the next 12 months with all occurrences of the event. Every month, go through all events and extend the "pre-fill" through another month (13 months hence).
Now the SELECTs are simple and fast.
SELECT ... WHERE date = '...'
has all the events for that day (assuming it is within 12 months).
The complexity is on inserting. But presumably you insert less often than you select.
The table with the event definitions would be only as complex as needed for your app to figure out what to do. Perhaps
start_date DATE,
frequency ENUM('day', 'week', 'month', ...)
multiplier TINYINT, -- this lets you say "every second week"
offset TINYINT, -- to get "15th of every month"
Twice a week would be two entries.
Better yet, there are several packages (in Perl, shell, etc) that provide a very rich language for expressing event-date-patterns. Furthermore, you may be able to simply 'call' it to do all the work for you!

SSRS compare week to the same week number from previous year

I am working on an SSRS report that gets its data from an OLAP cube. In the OLAP cube I have a field named WeekOfYear which gives me the week number of the year based on the date. For example, week 1 for January 1st (if January 1st falls on a Monday) and week 2 for January 8th. My data is grouped by this field but now I want to be able to compare the data from this week of the year to the previous year's week of the year. Like comparing Week 1 of 2015 to Week 1 of 2014. Is there anyway that I can accomplish this? I appreciate any help. Thanks.
There is a LookUp function that should be able to do what you need.
You mention you have a WeekOfYear field. This also assumes you have a Year field (or can calculate it with **YEAR(Fields!YouDateField.Value) )
=LookUp(Fields!YourYearField.Value - 1 & "|" & Fields!WeekOfYear.Value, Fields!YourYearField.Value & "|" & Fields!WeekOfYear.Value, Fields!YourValueField.Value, "YourDataSet")
If you are actually summing multiple rows of data from your cube, you would need to use LookUpSet to get all the values and sum them with a custom function (since Microsoft couldn't possibly envision users wanting to SUM multiple records). Luckily users have already created a function - SumLookup. See How to combine aggregates within a group with aggregates across groups within SSRS if needed.
If you have access to the OLAP cube and can edit this, you could define a new Calculated Measure on the cube. How exactly this would work depends on the set up of your date hierarchies. You can also access this and define calculated measures through the Query Designer while constructing your dataset in Report Builder/Visual Studio.
Right-click in the cube browser and choose "New Calculated Member".
Reporting on weeks across years can be difficult due to the fact that the number 7 doesn't fit neatly into 365 or 366, so you always end up with a little over 52 weeks. Since the 1st of January could be a Sunday one year, and a Tuesday on the next (2012/2013), it's not always a good idea to directly compare these. So people may work around this by defining the 7-day weeks for the year against their date dimension. One year you may have 52 weeks, another you'd have 53. This is a little off topic, so I'll link to an explanation of this here, but it is important to be aware of this in order to implement my suggestion below.
Assuming you have a nice hierarchy on your date dimension that can aggregate up Weeks to Year level, you can create a new measure in your cube using the ParallelPeriod function.
[Measures].[SalesSPLY] AS
(
ParallelPeriod
(
[Dim Date].[ReportingCalendar].[ReportingYear],
1,
[Dim Date].[ReportingCalendar].CurrentMember
),
[Measures].[Sales]
)
My example MDX assumes that you already have a hierarchy called ReportingCalendar created on your date dimension. Yours may be named differently.
Now if you browse your cube and select WeekOfYear, Sales, and SalesSPLY, you will see your value for this year's week 1, alongside last year's.
OLAP cubes are very good at this type of time-based intelligence, as they can very quickly provide aggregated and offset data in a way that would be slower to run in an RDBMS or within SSRS itself.

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

Access 2010 Query for current month

I have built an Access database to keep track of Quality Assurance Monitors for our team. Our team has several leads that oversee the reps on the team.
I want to build a query the will return all of the QA's for a specified lead for the current month. I have criteria for specifying a lead and have got that to work, but every time I try to set the criteria for the current month, it ends up returning no results.
Searching Google has repeatedly suggested using the Month(Now()), but that doesn't work either.
How can I write this query?
Answer inspired by #Justin-rentmeester but refined to always return the desired results:
Add the following to the WHERE clause of your query:
MyDateField BETWEEN
DateSerial(Year(Date()),Month(Date()),1)
AND
DateAdd("s", -1, DateAdd("m", 1, DateSerial(Year(Date()),Month(Date()),1))), DateSerial(Year(Date()),Month(Date()),1)
I reccommend that you use SQL view to add this criterium.
This approach also works with dates that include times, and with months with less than 31 days.
Put this code on criteria field- Between Date()-Day(Now()-1) And Date()
I edit my answer!
Assuming workdate is your date field.
A shorter where clause can be
where Year(workDate) = Year(Date()) and Month(workDate) = Month(Date())
This will filter all records for the current month.
If you want to further filter for records up to today add
and workdate <= Date()
This may not be necessary if you are not storing future dated records in your table.
Good luck.
To do this in the Design view in Access, select all of the columns you want in the report and on the date column you want to restrict to the current month, for the Criteria put: Between
DateSerial(Year(Date()),Month(Date()),1)
And
DateSerial(Year(Date()),Month(Date()),31)

Storing day and month (without year)

I'm having trouble with figuring out the best way to store some data in my database. I've got to store DD/MM dates in a database, but I'm not sure of the best way to store this so that it can be easily sorted and searched.
Basically a user will be able to save important dates in the format DD/MM, which they will be reminded of closer to the day.
The DATE data type doesn't seem completely appropriate as it includes year, but I can't think of another way of storing this data. It would be possible to include a specific year to the end of all occasions, but this almost doesn't seem right.
I've got to store DD/MM dates in a database, but I'm not sure of the best way to store this so that it can be easily sorted and searched.
The best way to store date data, even if the year component is not required, is to use date. When you need to use it, you can remove the year, or replace it with the year being compared against (or current year).
Having it in date column facilitates sorting correctly, integrity, validation etc.
To cater for leap years, use a year like '0004' which allows '0004-02-29'. Using year 4 makes it slightly more complicated than year 0, but as an example, this turns the date '29-Feb' (year agnostic) into a date in this year for comparison with some other field
select
adddate(
subdate(cast('0004-02-29' as date),
interval 4 year),
interval year(curdate()) year)
result: 2011-02-28
Are these dates recurring? If not, how will you keep track of when one has "expired"? If the answer is "the app will manually remove the dates once they have expired", then why not simply store the DD/MM date as the next available instance of that date? For example:
01/02 becomes 2012-02-01, and 04\07 becomes 2011-07-04
The built-in date/time functions are so useful that I strongly recommend you not use varchars or tinyints.
If you really really want to drop the year, then just make TWO columns, one for day and another for month. Then store them separately.
CREATE TABLE `table-name` (
`Day` tinyint NOT NULL,
`Month` tinyint NOT NULL
);
But, it's much better to just use the Date type and then ignore the year in your code.