I am trying to grab all the records for the month. The string I have to query with is in this format 2019-01-12. The record I am querying for is a DateTime record so it has a format like this 2018-08-11 13:39:22.959330.
So I am trying to structure a query that would achieve this
Score.where('user_id = ? AND date_closed.strftime("%Y-%m) = ?', current_user.id, date)
Where date is the 2019-01-12 string. The above code produces the error
FUNCTION date_closed.strftime does not exist
I did google but was unable to locate something that achieved this. Most solutions involved searching inside a range of dates, I would really like to try to keep the search method the same.
You have the DATE_FORMAT function for that https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
DATE_FORMAT(date_closed, "%Y-%m")
EDIT: you'll have to also format the date you are passing to the query
You can use mysql data functionos YEAR and MONTH:
Score.where('user_id = ? AND YEAR(date_closed) = ? AND MONTH(date_closed) = ?', current_user.id, date.year, date.month)
Related
I work for a gun club and we are trying to find the total number of targets shot at during a specific year. The table contains totals from years 2000-2018 and I am trying to write a query that will look at the string for the date of the shoot which is in a format like this 2010-06-13 00:00:00.000 I just care about the year so I created this query:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate LIKE '2007-%%-%% 00:00:00.000'
If I run the query up to the AND clause it works and returns a total. However, I get a null value if I highlight the whole thing. I have tried variations of the string as well. Such as this '2007%' and this '2007-- %'
Not sure what I am missing. Any help is appreciated.
Don't convert to string to query for a year, use YEAR() function instead:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND YEAR(ShootDate)=2007 -- MySQL
You could also use a range query
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate BETWEEN '2007-01-01' AND '2007-12-01 23:59:59.999'
Note: The above assumes that you do not store dates as strings. The function to use depends on RDBMS. In MS SQL Server you would use DATEPART(year, ShootDate) = 2007
I have table Birthdays.
Birthdays fields: id | user_id | birthday (type date: y-m-d)
Data from url $_GET['date'] is '1991-09-10'
In BirthdaysController i have a query
$arrayDate = $this->Birthday->find()->where(['birthday' => $_GET['date'] ])->all();
but i want to find by format '1991-09'.
Any help would be greatly appreciated.
Please help me!
I guess what you want to do is to find people with a birthday date for the month of the year given?
First of all, you should use the GET parameters like this $this->request->query('date'); and not like this $_GET['date'].
You'll have to use the between function, so to be sure you're using the first and last day of the month, convert the date like this :
$startDate = $endDate = new \DateTime($this->request->query('date'));
$startDate->modify('first day of this month');
$endDate->modify('last day of this month');
And then you just have to create a query like this
$arrayDate = $this->Birthday
->find()
->where(function($exp) {
return $exp->between('birthday', $startDate->format('Y-m-d'), $endDate->format('Y-m-d'), 'date');
});
They're other ways to use this between find conditions, you should read this.
How could i get only de date of a datetime column in laravel 5 using fluent?
I want to compare this 2015-08-30 23:00:00 with this:
$dt = Carbon::now();
$d = $dt->toDateString();//this is the date only which i want to compare
There is a mysql function called DATE which extract the date part of a date or datetime expression like this DATE(`datetime`) but if i use it it gives me a integrity error.
Here's how i tried to use it:
$reservations = \DB::table('reservations')
->select('reservations.id','DATE(reservations.datetime)')
->get();
Follow the code to get the all of record in the date '2016-07-14' by using it
whereDate('date','=','2016-07-14')
if your date is available in a variable $date then use the below code
whereDate('date',$date)
Nobody answered so i'll post how i solved it temporary:
Laravel provides something called Raw Expressions, so i what i did was use a raw select to get the date string of a datetime like this:
$reservations = \DB::table('reservations')
->select(\DB::raw('reservations.id, DATE(reservations.datetime)')
->get();
My goal was to compare a date from a datetime column with a Carbon date and here's how i did it:
$dt = Carbon::now();
$d = $dt->toDateString();//Only date from datetime
$reservations = \DB::table('reservations')
->whereRaw("DATE(reservations.datetime) = '".$d."'")
->select(\DB::raw('reservations.id, DATE(reservations.datetime)')
->get();
I used whereRaw to make a Raw sentence for the where clause.
If someone has a better way of do this same thing, maybe using Eloquent i would like to know it since Eloquent is a bit more secure...
The correct way to do it and what you are looking for is this:
$query->whereDate('created_at', '=', date('Y-m-d'));
See here for more details for whereDate():
http://laravel.com/api/5.0/Illuminate/Database/Query/Builder.html#method_whereDate
http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/
You can use whereDate for compare only date. flow bellow code.
whereDate('date', '=', $date)
If you give whereDate then compare only date from datetime field.
I have a query where I need to select entries only where their month is equal to a certain month.
Each entry has the date saved in the ymd format (20140211).
I have tried the following, but it doesn't recognise the month.
AND (MONTH(matrix.col_id_3) = '1' OR MONTH(matrix.col_id_4) = '1')
Is this due to the date format? I am stuck with this format as its part of the CMS I use.
Any help would be fantastic.
if you can use the default date column type (why?) so you have to convert your column
i.e.
... MONTH(STR_TO_DATE(matrix.col_id_3,'%Y%m%d')) = 1 ...
probably your dates are stored as strings, so you could use the following
.... SUBSTRING(matrix.col_id_3, 5, 2) = '01' ...
i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.