I am using mongodb(Mongoid) and rails 3.
I have a field "timestamp": ISODate("2013-02-07T04: 41: 52.773Z").
Now i want to search date in range means fromdate and todate variables.
Value of fromdate: "2013-02-01" and todate: "2013-02-08".
I am trying using where(:timestamp.gt => fromdate, :timestamp.lt => todate) but not get proper output.
How i can make a query to find data in date range..???
You will need to convert your "2013-02-01" to a Date object in Ruby. See http://www.ensta-paristech.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/date/rdoc/classes/DateTime.html#M000215
Related
I'm trying to find if there is a post from the same date.
#selected_date = Date.new(params["post"]["date(1i)"].to_i,params["post"]["date(2i)"].to_i,params["post"]["date(3i)"].to_i)
#existing_post = Post.where(user_id: current_user.id, date: #selected_date).first
My #selected_date contains only the date 2021-03-19, but my date db field contains time as well date: "2021-03-19 17:43:50.640258000 +0000"
In the Query how can I point only date section to compare? When I do , date: #selected_date) its trying to compare without time, so it fails.
You can do this using the all_day helper which creates a range covering the whole day.
Post.find_by(user_id: current_user.id, date: #selected_date.all_day)
There are at least two ways of doing it:
Honor the DB type, and query accordingly:
#existing_post = Post.where(
user_id: current_user.id,
date: #selected_date.beginning_of_day..#selected_date.end_of_day # use #selected_date.all_day from Eyeslanduc's answer: https://stackoverflow.com/a/66787291/299774
).first
This will query for entries with date between the day's 00:00:00 and 23:59:59
Cast the date column, which seems to be really a timestamp:
#existing_post = Post
.where(user_id: current_user.id)
.where("date_trunc('day', date)::date = ?", #selected_date).first
possibly .where("date::date = ?", #selected_date) would work the same.
This would cast the date column to date type, which should give you what you want.
I'm having a strange issue here with a query from which I'm trying to pull data queried by start and end date parameters.
I'm conducting the following.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String preparedQuery = "SELECT ID, FULNAME, FRMEMAIL, LOCATION,TYPE1,TYPE2,BORROWER_NAME,LOAN_NUM,TIME_REQ
FROM FORM_REDUCVU WHERE to_date(TIME_REQ,'yyyy-mm-dd') >= ? AND to_date (TIME_REQ,'yyyy-mm-dd') <= ? ORDER BY ID DESC";
java.util.Date util_StartDate = sdf.parse( request.getParameter("time_req_date") );
java.sql.Date timreq_datevar1 = new java.sql.Date( util_StartDate.getTime() );
java.util.Date util_EndDate = sdf.parse( request.getParameter("time_req_date2") );
java.sql.Date timreq_datevar2 = new java.sql.Date( util_EndDate.getTime() );
out.println("datestamp java time vals "+timreq_datevar1 + " " + timreq_datevar2);
PreparedStatement prepstmt = connection.prepareStatement(preparedQuery);
prepstmt.setDate(1, timreq_datevar1);
prepstmt.setDate(2, timreq_datevar2);
And the out.print values will give something like 2018-10-09 and 2019-02-20.
This does match the date picker selections I'm making, which are in mm/dd/yyyy format, i.e. 10/09/2018 and 02/20/2019.
Now the above returns no errors in the log, but no data either. But I did have the SQL query as "
WHERE to_datechar(TIME_REQ,'mm/dd/yyyy') >= ? AND to_char(TIME_REQ,'mm/dd/yyyy') <= ?
When changing to this above, I get the error in the topic of not a valid month.
I would have thought the SimpleDateFormat class would have parsed the date to mm/dd/yyyy in pattern, but it seems to make the java.slq.Date object pattern hyphenated, like yyyy-mm-dd.
Am I not parsing it correctly? Or is this going to require a different class or package, like java.Time? I would thought this would work, but there has to be some small conversion method not working.
Bottom line - I'd really prefer to keep the jQuery date pickers with their mm/dd/yyyy formats in tact, and be able to query by these entries.
Any input regarding this is welcomed.
Thank you!
Do NOT use to_date() on a column that is alread a DATE to_date() will convert the date to a varchar just to convert it back to a DATE which it was to begin with.
So your condition should be:
WHERE TIME_REQ >= ? AND TIME_REQ <= ?
You are correctly using setDate() so Oracle performs a date to date comparison - there is no need to convert the date in the database to a string value.
Ideally you should use java.time.LocalDate and setObject() instead of java.sql.Date though (however not all Oracle driver versions support that)
I receive this parameter: param[:date] = '2017-03-04'
and the column in my database is created_at with this format: '2017-01-01 09:21:23'
When I execute this query
Mymodel.where(created_at: params[:date])
It returns an empty array, and it is logic due the time that is not passed
I need all the rows that corresponds to the param date.
How can I do this? The time is not important in this case, I only need the date.
I am using postgres as db.
I need to select e.g. All Sell of a specific day and the column to search is created_at.
This is a type-coercion problem. Because the created_at field contains a date and time, your date is being converted into 2017-03-04 00:00:00 for the query. Anything that doesn't exactly match that timestamp will be excluded.
There are two approaches to solving this problem.
Database agnostic
Turn your date into a Range object. ActiveSupport provides the Date#all_day helper for this use-case.
date = Date.parse(params[:date])
MyModel.where(created_at: date.all_day)
Since Date::parse throws an exception if parsing fails, the real-world implementation would have to account for that
my_models = begin
date = Date.parse(params[:date])
MyModel.where(created_at: date.all_day)
rescue ArgumentError
MyModel.all
end
Postgres
You can cast your created_at field to date so that only the date part will be matched.
MyModel.where("created_at::date = ?", params[:date])
Good Day! I have this problem I have a code like this
tree_model = self.env['hr.reception'].search([('create_date','=',date_from)])
the date_from is only a date only parameter while the column create_date is datetime column I only want to format the create_date as date only format
is this possible just like in postgres
(create_date::date)
Thanks for the Help
You can achieve it using date() method which will return the date format instead of datetime.
For example:
1. datetime.now() will result in datetime format.
2. datetime.datetime.now().date() will result in date format.
Try : create_date.strftime('%Y-%m-%d')
I have a db with records that have a fromDate and toDate field.
For getting the active record i use :
datetime('now') between fromDate and toDate;
That works fine.
The thing is that the fields are not always populated.
I want that if the is no fromDate and no toDate i will always receive the record
If there is only a fromDate then after that date i will always receive the record I
If ther is only a toDate i will receive the record only until the toDate.
Thanks a lot
Avi
You can use logic like this:
where datetime('now') >= coalesce(fromDate, datetime('now'))
datetime('now') <= coalesce(toDate, datetime('now'))
I don't recommend using between for date/time values. Time components can sometimes produce unexpected results.