Mysql release year from date format

I need a mysql query to extract a year from the following date format from a table in my database.

For example,

subdateshow ---------------- 01/17/2009 01/17/2009 01/17/2009 01/17/2009 01/17/2009 

following request did not work

 select YEAR ( subdateshow ) from table 

The column type is varchar. Is there any way to solve this?

+42
mysql
Jan 03 2018-12-12T00:
source share
8 answers

Since your subdateshow is a VARCHAR column instead of the correct DATE, TIMESTAMP or DATETIME column, you need to convert the string to a 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

+55
Jan 03 2018-12-01T00:
source share
 SELECT EXTRACT(YEAR FROM subdateshow) FROM tbl_name; 
+10
Dec 28 '15 at 7:35
source share

You can try the following:

 SELECT EXTRACT(YEAR FROM field) FROM table WHERE id=1 
+6
Feb 18 '13 at 7:26
source share

This should work:

 SELECT YEAR(STR_TO_DATE(subdateshow, '%m/%d/%Y')) FROM table; 

eg:

 SELECT YEAR(STR_TO_DATE('01/17/2009', '%m/%d/%Y')); /* shows "2009" */ 
+4
Jan 03 '12 at 13:13
source share

try this code:

 SELECT DATE_FORMAT(FROM_UNIXTIME(field), '%Y') FROM table 
+3
Mar 21 '13 at 11:15
source share

This should work if the date format is consistent:

 select SUBSTRING_INDEX( subdateshow,"/",-1 ) from table 
+2
Jun 18 '13 at 17:23
source share

TRY:

 SELECT EXTRACT(YEAR FROM (STR_TO_DATE(subdateshow, '%d/%m/%Y'))); 
0
Jan 03 '12 at 13:13
source share

try this code:

SELECT YEAR( str_to_date( subdateshow, '%m/%d/%Y' ) ) AS Mydate

0
Jan 03 2018-12-01T00:
source share



All Articles