For this kind of problem it's usually much easier to implement a table with the required date information and join on tthat table, and filter using it. I.e. create a table with this info:

CREATE TABLE Dates(
  Date DATE PRIMARY KEY CLUSTERED,
  PositionInMoth TINYINT,
  LastInMonth BIT)


Then fill up this table using whichever method you want. I think you'll do it much easyly with a simple ad-hoc app, but you can also create it using a T-SQL script.

Then you simply need to join your table with this one, and use the PositionInMoth or LastInMonth columns for filtering. You can also use this as a lookup table to easyly implement the required function.

By the way, don't forget that there are many months which have a fifth instance of a given day, for example, on december 2014 there are 5 Mondays, 5 Tuesdays, and 5 Wednesdays. The number of days with 5 instances in a given motnh is: number of days in the month - 28, for example, in December it's 31-28 = 3. So you can't count on the 4th being the last.

This table really takes up very little space, roughly, 3 bytes for the DATE, 1 byte for the TINYINT, and 1 byte for BIT, so it's 3+1+1 = 5 bytes per day, 1,825 bytes per year, and 178 kb for a whole century. So, even if you needed several centuries to cover all your possible dates, i would still be a very small table. I say roughly because the index structure, the fill factor and some other things will make the table somewhat bigger. Being such an small table means that SQL Server can easyly cache the whole table in memory when executing the queries, so your they will run really fast.

NOTE: you can expand this table to cover other needs like checking if a day is the last in the month, or the last or first working day in a month, by adding new BIT columns

Very interesting link, from OP comment: CALENDAR TABLES IN T-SQL

SQL - Check if a date is the first occurrence of that day in its month
I download a ".ibooks" file for this free book and tried adding it to my iPhone library but it doesn't show up. Manually dragging it over doesn't work either.

Is it because .ibooks isn't supported by iPhone?
