PHP/MySQL targeting only month from date field - php

My table has a date column formatted as yyyy-mm-dd.
Is it possible to have a query that only lists results found for any day in a specific month? There could be any month of any year in this table, but say i want to list them for only june 2013?
Thanks

SELECT * FROM tablename WHERE date_column_name LIKE '2013-06%'

select * from your_table
where date_column between '2013-06-01' and '2013-06-30'

Related

Select items between two dates, varchar format

I have a table who contains a field 'date' which is a date in varchar format ("2016-07-30"). I would like to select all items which are between the date of today and the date of today +1 month. I already done the variable who contains the two dates (in date format) but I don't know how to select items between these two dates because the field of my table is in varchar and not in date format.
Thank you so much for your help.
Try something like this:
select *
from yourtable
where str_to_date(datecol, '%Y-%m-%d') between now() and date_add(now(), interval 1 month)
But the right thing you should do is alter your column type to date.

mysql php pdo select a part of row value date record

I want to select a part of a record in database. The record i want to select from is date.
The record date is this format 07/02/2015.
I want to have a select option where i can put option of year and month to used as parameter for the search.I want to know a select query for ff.
How can I select a specific year from the record(annual report)
I want to select all record in database with the current year, or with the year 2014.
How can i select a specific month from the record(monthly report)
I want to select all record in database with the current month and current year or with a X month and a X year
Use BETWEEN Keyword
SELECT * FROM tableName WHERE dateCol BETWEEN '01-01-2014' AND '31-12-2014';
for reference
http://www.w3schools.com/sql/sql_between.asp
Select * FROM table WHERE date between two selected dates
Please try
SELECT * FROM TABLENAME where YEAR(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = '2014'
SELECT * FROM TABLENAME where MONTH(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = x

SORT rows by date stored in Month form

Actually, I am trying to sort my rows by Date field in which the date is stored in the form of like
DateC
===========
April, 2012
May, 2012
January, 2013
Date
===========
2013-01-03 10:51:23
2013-02-19 10:51:23
2013-03-26 10:51:23
But i am not able to sort it simply by just using ORDER BY datec DESC.
I can't use ORDER BY FIELD (datec, 'December 2012', November 2012'...) because there are two dates stored in my database. One is the timestamp i.e date, and the other is custom date i.e. datec which the user enters by himself. If the datec field is empty, it outputs the date field.
So, what I want is it should order the rows by both the date columns...
Please Help. If you have some questions, please ask mee....
Thanks in advance...
I think this is what you are looking for, using STR_TO_DATE and COALESCE:
select *
from yourtable
order by
COALESCE(STR_TO_DATE(datec, '%M %Y'),date)
SQL Fiddle Demo
Date are stored in string format. so you need to use STR_TO_DATE() function
Try this
SELECT * FROM table ORDER BY STR_TO_DATE(datec, "%M, %Y") DESC
This is one advantage of storing dates as string on the database. The ordering can still be applied provided that you need to use STR_TO_DATE() function to convert the string into valid dates.
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC
SQLFiddle Demo
LINK
STR_TO_DATE()
if you have time to alter the schema, the best way to do is to store those dates as DATE or DATETIME on the database. If you worry about the formatting of value during SELECT statement, there is still a function that called DATE_FORMAT() which convert the date into string at your desired format.
DATE_FORMAT()
Example,
SELECT DATE_FORMAT(CURDATE(), '%M, %Y') DateC
will yield
March, 2013
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC,
date DESC
SQLFiddle Demo

SQL query to select values for a current or specific month in a given date/time format

I have saved the date in the database (mysql) as (d-m-Y H:i:s)
How can write a sql query to select all the values that are only for the current month or a specific month ?
SELECT *
FROM table
WHERE DATE_FORMAT(date_field,'%m-%Y') = '07-2012';
Store your dates in MySQL DATE format and use the following query:
SELECT
*
FROM
table
WHERE
MONTH(date_field) = MONTH(CURRENT_DATE)
AND
YEAR(date_field) = YEAR(CURRENT_DATE)
Storing dates as a string/varchar is a very bad idea.
SELECT * FROM `your_table` WHERE MONTH(`your_date_column`)=6
AND YEAR(`your_date_column`)=2012
Gets everything from June 2012.

Get Current year and next form mysql

Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!
SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;

Categories