Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, let us start learning!.
Table of Contents
What is the SQL DATE() function?
SQL DATE() function enables us to access the date value from the particular historical or current timestamp values.
Let us consider an example to understand the same.
Consider a small scale company, wherein a record of the attendance of the employees is being maintained in the form of a database. If the supervisor wishes to check for the days on which a particular employee is absent, the developer can run the DATE() function on the attendance database and help the supervisor with the date values of the same.
So, we can say that the DATE() function helps us extract the date values from the provided datetime expression.
Let us now understand the structure of DATE() function.
Syntax of DATE() function
The Date() function returns the date extracted from the passed datetime expression to it.
The function returns NULL, if the passed argument is not a date or datetime expression.
Having understood the working of DATE() function, let us now implement the same through some examples.
Implementing DATE() function through examples
In the below example, we have passed a datetime expression. The DATE() function extracts the date value from the expression.
SELECT DATE("2020-06-07 11:43:21");
In this example, we have violated the standard format of the datetime expression and thus the function returns NULL.
SELECT DATE(" DATE: 2020-06-07 11:43:21");
In this function, we can use the
SQL NOW() function to fetch the current datetime expression from the system. Further, we have applied DATE() function to extract the date part from it.
create table Info(id integer, Name varchar(200), Attendance DATE); insert into Info(id, Name,Attendance) values(1, "Pune", '2020-03-03 12:32:23'); select * from Info;
From the above data, we use the DATE() function to extract the date part from the datetime expression present in the table column.
Select Name, DATE(Attendance) from Info;
By this, we have come to the end of this topic. Feel free to comment below in case you come across any questions. Till then, Happy Learning!