how to write Rails model query between date - sql

This is my sql query I wont to convert into rails model query
sql : SELECT "vouchers".* FROM "vouchers" WHERE "vouchers"."business_id" = 31 AND '2014-08-20' between start_date and end_date;
My tried model query but this not working please help me to make it working
Voucher.where(["#{Date.today} BETWEEN ? AND ?",:start_date,:end_date])

Try in this format:
data = ModelName.where("today >= from_date AND today <= to_date")

Assuming that start_date and end_date are columns try this version
Voucher.where("current_date between start_date and end_date").where(business_id: 31)

Model.where("DATE(created_at) > :today AND DATE(promoted_till_date) < :two_weeks", today: Date.today, two_weeks: Date.today+14.days)

Related

DATE_ADD Function

A glitch in code on my website caused the start date and end date in an sql table to be set to the same day for my online member directory. I have the code glitch corrected, but now need a way to fix the data in the database. I am trying to add a certain number of months (12, 18, 24, etc) to the end_date column depending on the package column. I only want to do this when the start_date column is equal to the end_date column. I have a query started, but keep running into an invalid syntax message. Can someone help? My sql is below
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATE_ADD(months,12,end_date)
WHERE start_date = end_date
AND package_id = 1;
SET end_date = DATE_ADD(start_date, INTERVAL 12 MONTH)
This is for MySQL.
If I understand you correctly, you want to use DATEADD instead.
Your syntax seems like DATEADD (datepart , number , date ) which is not the syntax for DATE_ADD
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATEADD(month, 12 ,end_date)
WHERE start_date = end_date
AND package_id = 1;
Check more here

SQL querying with DATES - not pulling correct data

I'm running a simple query where I am trying to filter by a date, like this...
Select * from tblPurchasedProducts
WHERE PurchasedOn >= '11/04/2016' AND PurchasedOn <= '11/08/2016'
But I am not able to see any results for 11/8/2016. I'm only able to see everything prior to 11/08/2016.
The date is saved in SQL SERVER as smalldatetime. I'm really not sure if this has anything to do with it.
Is the time an issue in this case.
I am required to save the PurchasedOn date with as datetime
EDIT:
#lamak
WHERE PurchasedOn >= '20161104' AND PurchasedOn <= '20161108'
You need to be careful with your conditions. By using <= '11/08/2016' it's using the 00:00:00 time, that's why you are not getting results. Use:
SELECT *
FROM tblPurchasedProducts
WHERE PurchasedOn >= '20161104' AND PurchasedOn < '20161109';
Dates in SQL are formatted YYYY MM DD, so you would write '2016/04/11'
Unless you override it but you will need to specify the date format.
Try:
Select * from tblPurchasedProducts
WHERE PurchasedOn BETWEEN '2016-11-04' AND '2016-11-08'

SQL Query to comparing the minutes in SQL 2008 R2 server

What am trying to do is, I have table with Date field. Date field has entry in datetime format. Using that i have to display todays records after 5PM. Can anyone help me on this to get this query.
Thanks
After 5 Pm ?
Try this:
select *From Table where DATEPART(hh,yourdatefield)>=17 and and convert(date,yourdatefield,103)='2014-04-03'
this will the the records after 5pm .
Try this.
Select *
FROM tablename
WHERE datefield < '1/1/2014 17:00:00.000'
select * from table1
where
CONVERT(date,Datefield) = CONVERT(date,GETDATE()) and
CONVERT(time,Datefield) > convert(time,'17:00')

Rails ActiveRecord - how to fetch records between two dates

I have two date columns - from_date and to_date in a database table.
Example:
from_date: 2012-09-10
to_date: 2012-09-30
today: 2012-09-13
I need to fetch all records, if today's date is between from_date and to_date. How do I do that with a SQL query?
If I have loaded the respective record, I can easily decide, if today's date is between from_date and to_date, but I don't know how to fetch those records straight from the database table.
Check the Rails guides on Range conditions:
Client.where(created_at: (Time.now.midnight - 1.day)..Time.now.midnight)
That will produce the following SQL:
SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')
data = ModelName.where("today >= from_date AND today <= to_date")
A secure and easy way to do this would be:
Model.where(':date BETWEEN from_date AND to_date', date: Date.current)
you can use like this:
Data = Model.where("date(now()) between from_date and to_date")
data = ModelName.find(:all, :conditions => "today >= from_date and today <= to_date")
This should do the trick.
today = Date.today
data = ModelName.where("from_date <= ? AND to_date >= ?", today, today)
You could use below gem to find the records between dates,
This gem quite easy to use and more clear By star am using this gem and the API more clear and documentation also well explained.
ModelName.between_times(Time.zone.now - 3.hours, # all posts in last 3 hours
Time.zone.now)
Here you could pass our field also ModelName.by_month("January", field: :updated_at)
Please see the documentation and try it.
Ok, after a long search in google looking for a "rails way" to do a BETWEEN with two date fields and a variable, the most approximate solution I found is creating your own scope to do that.
in your ApplicationRecord write:
class ApplicationRecord < ActiveRecord::Base
scope :between_fields, -> (value, initial_date, final_date) {
where "('#{value}' BETWEEN #{initial_date} AND #{final_date})"
}
end
So now, wherever you need to do a between you can do:
#clients = Client.between_fields(params[:date], :start_date, :final_date)
# SELECT * FROM clients WHERE ('2017-02-16 00:00:00' BETWEEN start_date AND final_date)
You can combine it with others "where" to do your query as specific as you need.

SQL date Statement

I need some help figuring out and SQL Statement.
I know what I want I just cant express it.
Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.
Pseudo code
$query="SELECT * FROM MyTable WHERE 'TIS' is not older than 2 days or empty = ''$ORDER"; }
TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y).
The $ORDER is for ordering the values based on values from other fields not dates.
Im looking at
SELECT *
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");
But i dont quite think im on the rigth track with this.
Thanks
Try the following:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
$ORDER
I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
$ORDER
In MySQL try this:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
$ORDER
I hope this helps.
So, I was pretty lost in all this.
How did it got solved:
First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.
_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.
It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.
So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
and:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
Then proceeded to change the field value to date.
and this is my perfectly working query:
$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }
I would like to thank the posters for their aid.