how to use mysql extract year in where clause - mysql

i want to search on date field with year. i used Extract like this but it generates error.
SELECT * FROM (blog) WHERE EXTRACT(YEAR FROM blog_dt) = '2014'

you should do it like this:
SELECT * FROM (blog) WHERE EXTRACT(YEAR FROM blog_dt)= '2014' ;
or
SELECT * FROM (blog) WHERE YEAR(blog_dt)='2014';

It's much simpler:
SELECT * FROM (blog) WHERE YEAR(blog_dt) = 2014
See the manual:
YEAR
Returns the year for date, in the range 1000 to 9999, or 0 for the
“zero” date.
mysql> SELECT YEAR('1987-01-01');
-> 1987

You have an extra apostrophe after the EXTRACT() function, remove it and it should work (although #VMai's solution is much easier and more readable

You can use the Function DateFormat available in MYSQL.
select * FROM blog WHERE DATE_FORMAT(blog_dt,'%Y')='2014';
For more Date Related Functions visit URL

Related

Extract only the month in a query CodeIgniter

I want to make a query where I get people birthday's in a current month, I have problems with MONTH() it does not work for me, the date format in the database is: 04/18/1990, and I want to compare if the month current date('m') is equal to the month of the database.
Any suggestions?
you can try this one
SELECT * FROM table_name where extract(month from BirthDate) = date("m");
where date("m") return current month and BirthDate is column name in which you have stored birthdate.
Try this
"select month(str_to_date(birthdate), '%Y/%m/%d')
from users
where month(str_to_date(birthdate), '%Y/%m/%d') = '$month'"
Here, i m assuming your delimeter is '/', Please set your delimeter according to your string date i.e 2013/05/04 or 2013,05,04 or 2013-05-04
could be you have some convertion issue try using
select month(str_to_date(my_col, '%m/%d/%Y'))
from my_table
where month(str_to_date(my_col, '%m/%d/%Y')) = month(now())

mysql extract month from date format

My data value like below
date_time
----------------
24:Sep:2016 22:19:35
22:Oct:2016 22:19:35
26:Sep:2016 22:19:35
28:Sep:2016 22:19:35
Here I am using a query to check data where matched by month like below.
SELECT * FROM `audit_log` WHERE MONTH(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='Oct'
But this not working for me. But for year if I use that working
SELECT * FROM `audit_log` WHERE YEAR(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='2016'
I got my answer.
I used long form month to search and in query used MONTHNAME
SELECT * FROM `audit_log` WHERE MONTHNAME(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='October'
in different process I can use MONTH though it's return number. I have to use month number to search in query. like bwlow
SELECT * FROM `audit_log` WHERE MONTH(STR_TO_DATE(date_time, '%d:%M:%Y %H:%i:%s')) ='10'

How to compare the year to timestamp date

I want to take all the records from a specific year.
My table is race_details and the year is a part of the date in race_date (in unix timestamp)
SELECT *
FROM race_details AS r
WHERE r.race_date=(SELECT MAX(YEAR(FROM_UNIXTIME(race_date)))
FROM race_details)
Unless there's more to the problem, I think you're overcomplicating your query:
SELECT *
FROM race_details AS r
WHERE YEAR(race_date) = 2014;
Substitute 2014 for the year to look up, and if it's a variable, remember to parameterize!
You need to compare the year of the race_date, not the whole race_date:
WHERE YEAR(race_date) = (SELECT ...)

Retrieving all records based on a specific month in MySQL

my dates in my table are strings in the format:
"10/12/2009"
Now how would one get all the records from a month, lets say June (number "6" being provided)?
Check the MySQL function STR_TO_DATE.
You should not store dates as string, however. Use the type DATE.
The short answer to your question is that you can use the STR_TO_DATE and MONTH functions to 1) convert the string representation into a DATE, and 2) extract the month component from the date:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstringcol,'%M/%d/%Y')) = 6
(This is assuming here that by '10/12/2009', you are specifying Oct 12, and not Dec 10. You'd need to adjust the format string if that's not the case.)
Alternatively, if month is indeed the leading part of the date, you could do a simple string comparison, if the month is the leading component:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '6/%'
OR t.dateasstringcol LIKE '06/%'
(You could eliminate one of those predicates, if you have an exact format specified for the striing value representing the date: either if month is always stored as two digits -OR- the month is never stored with a leading zero.)
If you are passing in an argument for the month, e.g. '6', then you could construct your statement something like this:
WHERE t.dateasstringcol LIKE '6' + '/%'
If month is the second component of the date, then you could use:
SELECT t.*
FROM mytable t
WHERE t.dateasstringcol LIKE '%/' + '6' + '/%'
OR t.dateasstringcol LIKE '%/' + '06' + /%'
NOTE:
All of the previous example queries will return rows for June of any year (2009, 2010, 2011)
You can extend those examples, and do something similar with the year, using the YEAR function in place of the MONTH function, or for string comparison
AND t.dateasstringcol LIKE '%/%/2011'
Normally, we'd extract rows for a particular month for a particular year, using a date range, for example:
SELECT t.*
FROM mytable t
WHERE MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) >= '2011-06-01'
AND MONTH(STR_TO_DATE(t.dateasstring,'%M/%d/%Y')) < '2011-07-01'
Of course, when the date value is stored as a DATE datatype rather than as a VARCHAR, this means we don't need the STR_TO_DATE and MONTH functions, we can specify a comparison to the native DATE column. This approach allows us to make use of an index on the date column, which can improve query performance on large tables.
SELECT t.*
FROM mytable t
WHERE t.realdatecol >= '2011-06-01'
AND t.realdatecol < '2011-07-01'
The STR_TO_DATE function is your friend here:
SELECT * FROM my_table
WHERE STR_TO_DATE('10/12/2009','%M/%d/%Y') >= '2012-06-01';
MONTH should help here if we want current month or particular month data. e.g:
$month = date('m'); OR particular month.
SELECT * FROM users WHERE MONTH(str_to_date("10/12/2009",'%e/%m/%Y')) = $month;

Select by Month of a field

How can I get records from my table using month/year? I have a table like this:
Name - varchar
DueDate -datetime
Status -boll
DueDate is project due date, I want record corresponding to month/year, not full date, I mean record for specific month.
How can I do this in mysql?
Simply use MONTH() and YEAR():
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = 2010
You could use a between statement:
select * from Project
where DueDate between '2010-01-01' and '2010-02-01'
Do not use this here recommended solution with MONTH() and YEAR(). It prevents MySQL to use index on column DueDate if you have one and it forces MySQL to examine all rows in table.
Instead, use BETWEEN clause like this:
SELECT * FROM Project
WHERE DueDate BETWEEN '2010-01-01' AND '2010-02-01'
Or another solution with IN clause:
SELECT * FROM Project
WHERE DueDate IN ('2010-01-01', '2010-01-02', '2010-01-03', ..., '2010-01-31')
This two solutions allows MySQL to use index, so performance will be better.
SELECT Name FROM Table Where MONTH(datetime) = 1;
1 = January.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_month
it will select current year's specific month
SELECT * FROM Project WHERE MONTH(DueDate) = 1 AND YEAR(DueDate) = YEAR(NOW())
If you input month format "Y-m" you can use:
SELECT * FROM Project WHERE DATE_FORMAT(DueDate,"%Y-%m") = '2010-01'
You can extract the MONTH() and YEAR() for your DueDate.
SELECT * WHERE MONTH(DueDate) = '5' AND YEAR(DueDate) = '1987';
SELECT * FROM TABLE WHERE DueDate LIKE '2020-01%';
use in case you can substring your date data.