SQL get month from the date
Let us see few practical examples to understand this concept more clearly. We will use the MySQL database for writing all the queries. Then we will write the following query to create a table in the 'dbs' database: In the above query, the column named 'DateOfBirth' will store the date since the data type of this column is set as a 'DATE'. Now, we will write a query to insert records in the student table. We will execute the SELECT query to verify that all the records are inserted successfully in the student table. You will get the following table as output:
The above query results show that the date stored in the column 'DateOfBirth' is retrieved in the default format in which it is stored, i.e., 'YYYY-MM-DD'. Example 1: Write a query to retrieve only the specific part of the date, i.e., a month from the column 'DateOfBirth'. Query: Month () function is used in a SELECT query and applied on the column DateOfBirth to retrieve only the month from an entire date. 'MonthOfBirth' is the alias given using the AS keyword to store the month from the date. You will get the following table as output:
The results show that the birth month of all the students is successfully retrieved in the 'MonthOfBirth' column. Example 2: Write a query to retrieve only the specific part of the date, i.e., month, in a more readable format from the column 'DateOfBirth'. Query: You will get the following table as output:
DATE_FORMAT () function is used in a SELECT query and applied on the column DateOfBirth to retrieve only the month in a more readable format from an entire date. 'MonthOfBirth' is the alias given using the AS keyword to store the month from the date. Next TopicSavepoint in SQL |