Query to get a year from Varchar Data type

If a field contains date values with varchar data type, the following is the query to get year from that field

mysql> select identification_date from table1;

+———————+
| identification_date |
+———————+
| 11/05/2013          |
| 12/04/2012          |
| 02/06/2011          |
| 11/04/2012          |
| 11/04/2010          |
+———————+
5 rows in set (0.00 sec)

mysql > select YEAR(STR_TO_DATE(identification_date,’%d/%m/%Y’)) from table1;

+—————————————————+
| YEAR(STR_TO_DATE(identification_date,’%d/%m/%Y’)) |
+—————————————————+
|                                              2013 |
|                                              2012 |
|                                              2011 |
|                                              2012 |
|                                              2010 |
+—————————————————+
5 rows in set (0.03 sec)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s