You can get year out of timestamp using YEAR() function. The syntax is as follows −
SELECT yourColumnName FROM yourTableName WHERE YEAR(yourTimestampColumnName)='yourYearValue’';
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table getYearOut -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(10), -> yourTimestamp timestamp default current_timestamp, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.56 sec)
Insert some records in the table using INSERT command−
mysql> insert into getYearOut(Name,yourTimestamp) values('John',now()); Query OK, 1 row affected (0.26 sec) mysql> insert into getYearOut(Name,yourTimestamp) values('Carol','2018-09-23 17:34:44'); Query OK, 1 row affected (0.38 sec) mysql> insert into getYearOut(Name,yourTimestamp) values('Bob','2016-05-12 16:12:34'); Query OK, 1 row affected (0.43 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from getYearOut;
The following is the output −
+----+-------+---------------------+ | Id | Name | yourTimestamp | +----+-------+---------------------+ | 1 | John | 2019-02-05 11:46:11 | | 2 | Carol | 2018-09-23 17:34:44 | | 3 | Bob | 2016-05-12 16:12:34 | +----+-------+---------------------+ 3 rows in set (0.00 sec)
Here is the query to get year out of timestamp. If your table has a specific year, then the query is as follows −
mysql> select Id, Name from getYearOut where year(yourTimestamp)='2019';
The following is the output displaying the record of the year 2019 −
+----+-------+ | Id | Name | +----+-------+ | 1 | John + 1 row in set (0.00 sec)