I need a mysql query to extract the year from the following date format from a table in my database.
For eg :
subdateshow ---------------- 01/17/2009 01/17/2009 01/17/2009 01/17/2009 01/17/2009
the following query didn't working
select YEAR ( subdateshow ) from table
The column type is varchar. Is there any way to solve this?
You can use year() function in sql to get the year from the specified date.
The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well.
There are two SQL function to do it: DATEPART() YEAR() and MONTH().
Since your subdateshow is a VARCHAR column instead of the proper DATE, TIMESTAMP or DATETIME column you have to convert the string to date before you can use YEAR on it:
SELECT YEAR(STR_TO_DATE(subdateshow, "%m/%d/%Y")) from table
See http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
SELECT EXTRACT(YEAR FROM subdateshow) FROM tbl_name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With