What is SQL DATE() function?

Filed Under: SQL
SQL DATE() Function

Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, let us start learning!.


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.

DATE(datetime expression)

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");

Output:

2020-06-06T22:00:00.000Z

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");

Output:

NULL

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.

SELECT DATE(NOW());

Output:

2020-07-12T22:00:00.000Z

Now, we have created a table ‘Info’ using SQL Create command and have inserted a record in the table using Insert command.

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;

Output:

SQL DATE() Function--Table Info
SQL DATE() Function–Table 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;

Output:

SQL DATE() Function Example
SQL DATE() Function Example

Conclusion

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!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages