date select showing days before and after given date - ruby-on-rails-3

I want to have a drop down on a form that shows in plain English "1 day before, 2 days before" etc a given date as well as "1 days after, 2 days after".
How can I do this with Rails date_select?
Thanks
Ok I am creating my own as follows but am a bit stuck:
<%= select_tag(:board,options_for_select([["On the Birthday", 0], ["1 Day Before", -1], ["Two Days Before", -2], ["Three Days Before", -3]]), { :class => "default" }) %>
Now what I want to do is to show the number of days the user has saved. So how can I program or set the select so it knows which value to select.
I have the delivery date saved in a table and can access it as follows:
#board.deliver_on
Thanks.

Put simply: you can't do it with a date_select. You would need to build it using either the select or collection_select helpers and work out the values for the dates yourself. There is no helper in Rails to do that as far as I am aware.

Related

How to create a SQL date range query where the date range depends on holidays and weekends?

I would like fetch all records in Postgres that match a particular date range. The column 'date' is of type Date.
Return all rows where date is tomorrow's date
If today is a Friday, fetch rows that are Saturday, Sunday and Monday
If Monday is a holiday (ex: July 4), on Friday (7/1), we will fetch transactions for Saturday (7/2), Sunday (7/3), Monday (7/4) and Tuesday (7/5)
There may be multiple holidays in tandem and the logic needs to take that into account. Assume the holidays are all available as an array of dates.
My current solution is to blindly fetch all rows that match tomorrow, and then using code (the app is Ruby on Rails) perform the above logic and look ahead if necessary.
Is there an elegant SQL solution to the above problem?
If you are able to create array of dates to fetch in ruby, as you should be you are doing that already right now to figure out if you need to fetch anything else after tomorrow, you should be able to call something like this:
array_of_dates = [Date.new(2022, 4, 30), Date.new(2022, 5, 1), Date.new(2022, 5, 2)]
MyModel.where(date: array_of_dates).order(:date)
That should be enough. It gets translated to SQL like this:
select * from my_models
where date IN ('2022-04-30', '2022-05-01', '2022-05-02')
order by date
Order is not necessary.

SQL ORACLE Get week numbers from multiple datetime rows

I have 70.000 rows of data, including a date time column (YYYY-MM-DD HH24-MM-SS.).
I want to split this data into 3 separate columns; Hour, day and Week number.
The date time column name is 'REGISTRATIONDATE' from the table 'CONTRACTS'.
This is what I have so far for the day and hour columns:
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour"
FROM CONTRACTS;
I have seen the options to get a week number for specific dates, this assignment concerns 70.000 dates so this is not an option.
You (the OP) still have to explain what week number to assign to the first few days in a year, until the first Monday of the year. Do you assign a week number for the prior calendar year? In a Comment I asked about January 1, 2017, as an example; that was a Sunday. The week from January 2 to January 8 of 2017 is "week 1" according to your definition; what week number do you assign to Sunday, January 1, 2017?
The straightforward calculation below assigns to it week number 0. Other than that, the computation is trivial.
Notes: To find the Monday of the week for any given date dt, we can use trunc(dt, 'iw'). iw stands for ISO Week, standard week which starts on Monday and ends on Sunday.
Then: To find the first Monday of the year, we can start with the date January 7 and ask for the Monday of the week in which January 7 falls. (I won't explain that one - it's easy logic and it has nothing to do with programming.)
To input a fixed date, the best way is with the date literal syntax: date '2017-01-07' for January 7. Please check the Oracle documentation for "date literals" if you are not familiar with it.
So: to find the week number for any date dt, compute
1 + ( trunc(dt, 'iw') - trunc(date '2017-01-07', 'iw') ) / 7
This formula finds the Monday of the ISO Week of dt and subtracts the first Monday of the year - using Oracle date arithmetic, where the difference between two dates is the number of days between them. So to find the number of weeks we divide by 7; and to have the first Monday be assigned the number 1, instead of 0, we need to add 1 to the result of dividing by 7.
The other issue you will have to address is to convert your strings into dates. The best solution would be to fix the data model itself (change the data type of the column so that it is DATE instead of VARCHAR2); then all the bits of data you need could be extracted more easily, you would make sure you don't have dates like '2017-02-29 12:30:00' in your data (currently, if you do, you will have a very hard time making any date calculations work), queries will be a lot faster, etc. Anyway, that's an entirely different issue so I'll leave it out of this discussion.
Assuming your REGISTRATIONDATE if formatted as 'MM/DD/YYYY'
the simples (and the faster ) query is based ond to to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW')
(otherwise convert you column in a proper date and perform the conversio to week number)
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour",
to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW') as "Week"
FROM CONTRACTS;
This is messy, but it looks like it works:
to_char(
to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS') +
to_number(to_char(trunc(to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS'),'YEAR'),'D'))
- 2,
'WW')
On the outside you have the solution previous given by others but using the correct date format. In the middle there is an adjustment of a certain number of days to adjust for where the 1st Jan falls. The trunc part gets the first of Jan from the date, the 'D' gets the weekday of 1st Jan. Since 1 represents Sunday, we have to use -2 to get what we need.
EDIT: I may delete this answer later, but it looks to me that the one from #mathguy is the best. See also the comments on that answer for how to extend to a general solution.
But first you need to:
Decide what to do dates in Jan before the first Monday, and
Resolve the underlying problems in the date which prevent it being converted to dates.
On point 1, if assigning week 0 is not acceptable (you want week 52/53) it gets a bit more complicated, but we'll still be able to help.
As I see it, on point 2, either there is something systematically wrong (perhaps they are timestamps and include fractions of a second) or there are isolated cases of invalid data.
Either the length, or the format, or the specific values don't compute. The error message you got suggests that at least some of the data is "too long", and the code in my comment should help you locate that.

SSRS count working days only

I need some help in this my case is
1-two parameters date from , date to
2-number of team parameter that manually enter by user for later on use in some calculation
rquirement
count only working days (6days per week ) without Friday based on filtered period (date from and date to)
Code
=(COUNT(IIF(Fields!Job_Status.Value="Closed",1,Nothing))) /
((DateDiff(DateInterval.day,Parameters!DateFrom.Value,Parameters!ToDate.Value
)) * (Parameters!Number_of_teams.Value))
Note
this code is working fine but it calculate all days
thanks in advance
Try this:
=(DATEDIFF(DateInterval.Day, CDATE("2016-02-14"), CDATE("2016-02-17")) + 1)
-(DATEDIFF(DateInterval.WeekOfYear, CDATE("2016-02-14"), CDATE("2016-02-17")) * 2)
-(IIF(WeekdayName(DatePart(DateInterval.Weekday,CDATE("2016-02-14"),FirstDayOfWeek.System))="sunday",1,0)
-(IIF(WeekdayName(DatePart(DateInterval.Weekday,CDATE("2016-02-17"),FirstDayOfWeek.System))="saturday",1,0)
))
It will ruturn count of monday to friday between the given range in the above case it returns 3. For StartDate = 2016-02-14 and EndDate = 2016-02-21 it returns 5.
UPDATE: Expression to exclude friday from the count.
=(DATEDIFF(DateInterval.Day, Parameters!DateFrom.Value, Parameters!ToDate.Value) + 1)
-(DATEDIFF(DateInterval.WeekOfYear, Parameters!DateFrom.Value, Parameters!ToDate.Value) * 1)
-(IIF(WeekdayName(DatePart(DateInterval.Weekday,Parameters!ToDate.Value,FirstDayOfWeek.System))="friday",1,0))
Tested with:
DateFrom ToDate Result
2016-02-12 2016-02-19 6
2016-02-12 2016-02-18 6
2016-02-12 2016-02-15 3
It is very strange to me see a saturday and sunday as working days instead of friday.
Let me know if this helps you.
The most sustainable solution for this kind of question, in the long term, is to create a "date dimension" aka "calendar table". That way any quirks in the classification of dates that don't conform to some neat mathematical pattern can be accommodated. If your government decides to declare date X a public holiday starting from next year, just add it to your public holidays column (attribute). If you want to group by say "work days, weekends, and public holidays" no need to reinvent the wheel, just add that classification to the calendar table and everyone has the benefit of it and you don't need to worry about inconsistency in calculation/classification. You might want the first or last working day of the month. Easy, filter by that column in the calendar table.

How to use conditions in SQLite (like if-statements, etc.)

I have a table in a database with one column containing dates and another one containing scores. Basically, what I want to do is grab the best score in a given week.
Weeks can start on any given day (From Friday to Thursday, for instance), and that is defined by the user.
Here is what I have so far:
SELECT MAX(Series), DATE(DATE(Date, 'weekday 0'), '-7 days') dateStartOfWeek FROM SeriesScores
WHERE Season = '2010-2011'
AND dateStartOfWeek = '2010-08-29'
GROUP BY DateStartOfWeek
Where Series is the column containing the scores and Date is the (badly) named actual date.
The problem with this query is that it works for every day except for the day the week is supposed to be starting on.
For example: 2010-08-29 is a Sunday and in this example, I'm trying to find on which date the Sunday of the given week is. My function works for every day of that week except for 2010-08-29 (Sunday) since it tries to find the next day that is a Sunday (itself in this case). To compensate for that, I go back 7 days to get the correct Sunday, which creates the error for the already correct Sunday since this one doesn't need to go back 7 days or else it is one week off.
I figured I could solve this problem easily using Java, but I want to see how it should be done using SQL instead.
My solution (I don't even know if it can be done), would be to check if date and dateStartOfWeek are the same. If they are, don't substract 7 days from the date. If they're not, do as I did in my example. I don't know how to use conditions such as this one in SQL, though, and this is where I need help.
Thanks a lot in advance!
I think you need to use CASE operator - see http://sqlite.awardspace.info/syntax/sqlitepg09.htm
EDIT - try:
SELECT MAX(Series), CASE WHEN STRFTIME ( '%w', Date ) = 0 THEN DATE(Date, 'weekday 0') ELSE DATE(DATE(Date, 'weekday 0'), '-7 days') END AS dateStartOfWeek FROM SeriesScores
WHERE Season = '2010-2011'
AND dateStartOfWeek = '2010-08-29'
GROUP BY DateStartOfWeek
see http://www.sqlite.org/lang_datefunc.html

Rails 3 Finding day of week

I'm trying to make my own calendar because i cant seem to get any help with event calendar so what im trying to do is Display the day of the week ie.. January 1 2011 is a Saturday is there an easy command to get that day or do i need something else
You can also get the string, like "Wednesday", using time.strftime("%A")
Can't you use time.wday, 0 = sunday and so on
Actively using in Rails 4 - should in most other versions as well...
Both of these options will give you the day string (eg. "Saturday") for the date specified:
Specific date:
Date.new(2011, 1, 1).strftime('%A') # returns "Saturday"
Today:
Date.today.strftime('%A')
If you're attempting to write your own calendar from scratch and want to write a function to do day lookup, you might want to check out Conway's Doomsday Algorithm, which is an interesting method for determining the day of the week on any given date. Otherwise the standard time class has a wday method which returns a number from 0-6 (0 is Sunday, 1 is Monday, etc).
We can use Time.now.utc.wday to get day of week without considering zone.
No need to use the Time class. date.cwday will return 1 for Monday, 2 for Tuesday etc.