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.
Related
How should I match today's / current date irrespective of the time with column having date as (yyyy-mm-dd HH:mm:ss) eg (2019-04-05 16:00:00.0)
Using Grails GORM findBy method?
You could use between.
def today = new Date().clearTime()
def tomorrow = use( TimeCategory ){ today + 1.day }.clearTime()
YourDomain.findByYourDateBetween( today, tomorrow )
Probably more suitable for findAllBy.
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])
I have 2 fields in my DB2 database: DATE and TIME. DATE is string (8/25/2013), DATE is timestamp (1/1/1970 7:00:00 AM). I want to write a sql to replace 1/1/1970 with 8/25/2103. Please help.
Thanks.
Well, you need to cast your string to a date, using the DATE function
DATE(DATE)
Where you see that's it's a bad idea to name your columns with existing function names...
Then combining the TIMESTAMP, DATE, and TIME function (again really unclear with your field names, but...)
TIMESTAMP(DATE(DATE), TIME(TIME))
Will give you a timestamp where the date part is coming from the DATE field, and the time part from the TIME field
See this and that
After casting to a DATE format, adding the difference between those two absolute dates should work -
UPDATE <table-name>
SET DATE= DATEADD(day,DATEDIFF(day,'1/1/1970','8/25/2013'),DATE)
WHERE DATE < '1/2/1970' AND DATE>='1/1/1970'
More on DATEADD and DATEDIFF
Also, it is recommended to not use 'DATE' as a column name as it is a reserved [edit] function name.
Say I want record of all employees of date "07/03/2013" where format is "MM/dd/yyyy".
my expression in linq will be :
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date="07/03/2013")
here how linq manage the date format as "MM/dd/yyyy". OR how to compare "f.Date" with MM/dd/yyyy ?
Say,
if i does
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date.ToString("MM/dd/yyyy")="07/03/2013")
It does not allow me. suppose my date will "07/13/2013" then we can consider it matches and sync the format with "f.Date" but what about date is under 12 ? In fact by using first expression, m getting march month records, rather July.
How to figure out this issue?
You are comparing string representations of the date/time, which depend on the current system's formatting options. Use a date literal (must be in 'M/d/yyyy') to create a new DateTime object:
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date=#3/7/2013#)
If it's a variable, say dte:
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date=dte)
It should be this:
_dbContext.EmployeeDetails.Where
(Function(f) f.EmpId = _empId And
f.Date.ToString("dd/MM/yyyy")="07/03/2013")
You where passing Month first, so it parses March. Change toString order so you pass days before and then month. (July)
I see in the Active Record docs, you can query for a date using a greater than / less than comparison. However, what if you want to select where date = Date.today or must I query where date is greater than yesterday and less than tomorrow?
As you can see, I'm doing exactly that in the following queries and querying where Date = today returns an empty set
1.9.3p286 :096 > Subscription.where("created_at = ?", Date.today).count
(0.5ms) SELECT COUNT(*) FROM `subscriptions` WHERE (created_at = '2013-01-18')
=> 0
vs.
1.9.3p286 :098 > Subscription.where("expiration_date < ? AND expiration_date > ?", Date.today + 1, Date.today - 1).count
(0.4ms) SELECT COUNT(*) FROM `subscriptions` WHERE (expiration_date < '2013-01-19' AND expiration_date > '2013-01-17')
=> 1
Is this the proper way to query for today's date or am I missing something?
I think this is a DUP of this question:
How to select date from datetime column?
Subscription.where("DATE(created_at) = ?", Date.today).count
I'm pretty sure this works in MySQL and PostgreSQL, but I'm not sure if it's a SQL standard.
Wikipedia seems to think TO_DATE would be the standard:
http://en.wikipedia.org/wiki/SQL#Date_and_time
That didn't work for me in PostgreSQL though.
I believe the following post is more relevent to you
Rails ActiveRecord Find / Search by Date
In your case, when you search with date with activerecord you might want to take the timezone conversions into consideration.
When you use the below method
Subscription.where("DATE(created_at) = ?", Date.today).count
it uses Mysql's DATE function to parse the date and you will get UTC time as saved in Mysql by activerecord. If your rails app is using some other time zone, you will get wrong results.
The correct way to use this would be avoid using any SQL functions, and instead use a range.
Subscription.where(created_at: Date.today.beginning_of_day..Date.today.end_of_day)
This would fetch results with timezone conversions applied.
Please let me know you anyone has a better solution for this.
For me just works like this:
#fisrt_payment_day = Invoice.where("DATE(date_first_payment) >= ?", Date.today - 1.day)