- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the record of a specific year out of timestamp in MySQL?
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)
Advertisements