Select from MySQL datetime field where the year doesn't matter - mysql

I have a MySQL DateTime field which represent opening times.
I want to write a statement that will allow me to select rows from my table independent of the year supplied (Ex. *-12-17 00:00:00)

Try this:
SELECT * FROM table1
WHERE DAYOFMONTH(datecolumn) = 17
AND MONTH(datecolumn) = 12
Reference for DAYOFMONTH.

You did not say what you wanted to choose by if not the year: Go to the MySQL page and choose the correct functions.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Related

how to search month with year in mysql

I have a table. It has a varchar type date column.
I want to search that table using date, but i want to search only from month with year.
example: 12-2016
How can I write the query for that?
I want that part to assign to a variable of jasper
**example:**WHERE ............. =$P{invDate}
example as in my jasper report:
SELECT invoiceheader.inNo AS invoiceheader_inNo, invoiceheader.cusID AS invoiceheader_cusID, invoiceheader.soNo AS invoiceheader_soNo, invoiceheader.total AS invoiceheader_total, invoiceheader.dis AS invoiceheader_dis, invoiceheader.netTotal AS invoiceheader_netTotal, invoiceheader.cash AS invoiceheader_cash, invoiceheader.bal AS invoiceheader_bal, invoiceheader.date AS invoiceheader_date FROM invoiceheader invoiceheader WHERE ...........................................=$P{invDate}
I need a perfect query for the above space
Try this (you have used varchar data type for date so like keyword will perfectly suitable for this situation):
select * from table where date like concat('%' ,$P{invDate});
Try this case:
STR_TO_DATE(date_field, '%d-%m-%Y') < STR_TO_DATE('12-2016', '%m-%Y')
Try following -
select * from Table_name where month(date_column_name)=12 and year(date_column_name)=2016

MySql select Mix and Max year

Hi guys I am developing a php and mysql application. I want to select Min and Max from year. It should show correct year like below:
Min = 1998
Max = 2014
My table structure like below
Table: Students
year varchar(32) utf8_unicode_ci
-------------------------------
06-04-2012
02-05-2014
19-04-1999
05-05-2014
06-04-1998
I try to use MySql Min and Max but not working well.
SELECT MIN(`year`) AS Year FROM `Students`
SELECT Max(`year`) AS Year FROM `Students`
Please help me with this issue.
You should not store dates as strings. You should store them as dates.
But, because you have this format, you can use the right() function to get the year:
select min(right(year, 4)), max(right(year, 4))
from students;
If, for some unfathomable reason, you do have to store dates as strings, then use the ISO standard YYYY-MM-DD format.
If you want the minimum of the entire values as a date, then convert it to a date:
select min(str_to_date(year, '%d-%m-%Y')), max(str_to_date(year, '%d-%m-%Y'))
from students;
But, you should be storing the date using the proper data type in the first place.
Use the YEAR() function rather like
SELECT MIN(YEAR(`year`)) AS Year FROM `Students`

mysql query in between dates not working

I have the following mysql table:
tasks:
=====================
tid
status
desc
duedate
And i have the following records in that table:
records
===========================
1
active
Test description
08/15/2014
2
active
Another description
08/31/204
I am trying to get the days that there is a task for, in that particular month. I have the following query but when i run it it gets both records but "day" is null on both of them for some reason. Can someone please help me with this.
MYSQL QUERY
====================
SELECT DATE_FORMAT(due_date,'%d') AS day FROM tasks WHERE due_date BETWEEN '08/01/2014' AND '08/31/2014'
Try:
SELECT DAY(due_date) AS day
FROM tasks
WHERE due_date >= '2014-08'
AND due_date < '2014-09';
DAY() is a better function for what you want and I prefer using >= and < than BETWEEN for date comparisons, as it allows you to specify precise ranges more easily. Here, for example, you don't need to know the number of days in the month.
I have also used the default date format, which is preferable. If you need the, in my opinion, cray American date format, use DATE_FORMAT() in your SELECT.
This will only work with DATE, DATETIME and TIMESTAMP columns, which is how your due_date should be stored, preferably DATE.
UPDATE
To convert the VARCHAR column to DATE run:
UPDATE tasks SET due_date=STR_TO_DATE(due_date,'%m/%d/%Y')
Then change the type. Also remember to change your INSERT statements to use the default format.
You've got to convert those "date" strings to proper date values with STR_TO_DATE:
SELECT
DAY(STR_TO_DATE(due_date,'%m/%d/%Y')) AS day
FROM tasks
WHERE
STR_TO_DATE(due_date, '%m/%d/%Y')
BETWEEN STR_TO_DATE('08/01/2014' '%m/%d/%Y')
AND STR_TO_DATE('08/31/2014', '%m/%d/%Y')
else you're comparing strings instead.
Note:
It would be better to use a proper DATE or DATETIME column instead.
With the current VARCHAR format MySQL is unable to use indexes. That's very bad for performance.
You can convert your data by adding another column to your table:
ALTER TABLE tasks
ADD COLUMN new_due_date DATE;
Then you use an UPDATE statement to fill this new column
UPDATE tasks
SET new_due_date = STR_TO_DATE(due_date, '%m/%d/%Y');
If you don't need your old column anymore then you can delete this column and modify the new column to have the name of the old one. Then you will have your table with all your data in a DATE column.

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'

mysql getting all possible months from datetime field

so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12.
Is there a way to that ???
If you are looking at the list of all Months present in the created field (as I understand your query) then do this:
SELECT DISTINCT(MONTH(created)) FROM posts;
The resulting set would be the list of unique months in the field. If this will complain then try:
SELECT DISTINCT(MONTH(DATE(created))) FROM posts;
You can then substitute MONTH for MONTHNAME and get names instead. I did not add the WHERE clause to these queries but you can limit the dataset you are looking at as you see fit.
For more information take a look at http://dev.mysql.com/doc/refman/5.1/en/functions.html this has a list of quite a few functions that MySQL natively provides.
Yes, use the DATE_FORMAT function and other date and time functions.
More details here
For example, if you want all your records for December 2011:
SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12