Updated March 13, 2023
Introduction to DB2 current date
Database management systems provide a different kind of function to the user; the current date () function is one of the functions that is provided by the DB2 to the users. It is one kind of special register that is useful to fetch the system’s current date when we execute the SQL statement on the server. Basically, DB2 current date () function depends on the clock of the system. The CURRENT DATE () is a function that we called a special register, and it is used for more than one time within a specified SQL statement to return the CURRENT DATE or CURRENT TIME by using a single SQL statement.
Syntax
select current date from the system table or specified table;
Explanation
In the above, we use the current date () function to return the current system date; in this syntax, we can also use a system table that contains the value of the DB2 register, as well as we can also use this function with different clauses as per the requirement.
How DB2 current date function works?
Now let’s see how the current date () function works in DB2 as follows.
Basically, we can use different parameters for the current date () function as follows.
Year: It is used to return the current year.
Month: It is used to return the current month.
Day: It is used to return to the current day.
Day Name: This is used to return the day of the date.
Dayofyear: It is used to return the day of the year.
The CURRENT DATE exceptional register determines a current date that depends on a system date when the SQL statement will be executed on the current server.
On the off chance that this unique register is utilized more than one time inside a solitary SQL statement or utilized with CURRENT DATE or CURRENT TIME inside a solitary proclamation, all qualities depend on a solitary clock reading.
The estimation of CURRENT DATE in a client characterized work or put away strategy is acquired by using the standards.
Indicating CURRENT DATE is identical to determining CURRENT DATE. On the off chance that you need a timestamp with a predetermined accuracy, the extraordinary register can be referred to as CURRENT DATE(integer), where the whole number can go 0 – 12. The default accuracy is 6. SYSDATE can likewise be determined as an equivalent word for CURRENT DATE.
Examples of DB2 current date
Now let’s see the different examples of the current time to better understand as follows.
First, we need to create a database and table by using the following statement as follows.
create databases up;
Explanation
In the above example, we create a database statement to create a new database name. After that, we need to connect the database by using the following command as follows.
connect to up
Explanation
The end out we illustrate by using the following screenshot as follows.
Now perform the current date () function as follows.
SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1
Explanation
In the above example, we use the current date function to fetch the current date from the sysdummy1 table that is created inside the sysibm database. Basically, those are system-created databases and tables. The end out we illustrate by using the following screenshot as follows.
Suppose we need to know what yesterday’s data is at that time; we can use the following statement as follows.
select date ( days( current date) -1 ) yesterday_date from sysibm.sysdummy1
Explanation
In the above example, we use a select clause with the current date function; see here day-1 to determine the yesterday date from the sysdummy1 table that is created inside the sysibm database. Basically, those are system-created databases and tables. The end out we illustrate by using the following screenshot as follows.
In the same way, we can determine tomorrow’s date by using the same statement; we just need to use day+1 instead of day-1 as follows.
select date ( days( current date) +1 ) tommarow_date from sysibm.sysdummy1
Explanation
In the above example, we use a select clause with the current data function; see here we day+1 to determine tomorrow’s date from the sysdummy1 table that is created inside the sysibm database. Basically, those are system-created databases and tables. The end out we illustrate by using the following screenshot as follows.
Suppose we need to calculate the difference between the two dates at that time; we can use the following statement as follows.
SELECT (current date) - date(2020-05-03) FROM sysibm.sysdummy1
Explanation
In the above example, we try to find out the difference between the two different dates here. We use the current date function () with the second date. In this example, we perform the subtraction operation to determine the difference between the two dates from the sysdummy1 table that is created inside the sysibm database. Basically, those are system-created databases and tables. The end out we illustrate by using the following screenshot as follows.
Suppose we need to determine the current day at that time; we can use the following statement as follows.
SELECT (day(current date))present_day FROM sysibm.sysdummy1
Explanation
In the above example, we use the current date function with the day parameter, as shown in the above statement, from the sysdummy1 table that is created inside the sysibm database. Basically, those are system-created databases and tables. The end out we illustrate by using the following screenshot as follows.
Similarly, we can calculate month and year as follows.
SELECT (month(current date))present_month FROM sysibm.sysdummy1
Explanation
The end out we illustrate by using the following screenshot as follows.
The same function that is the current date function () we can apply on the user-created table.
So in this way, we can use the current date function as per the user requirement.
Conclusion
We hope from this article you learn the DB2 current date functions. From the above article, we have learned the basic syntax of a current date function, and we also see different examples of the current date function. Furthermore, from this article, we learned how and when we use the DB2 current date function.
Recommended Articles
This is a guide to DB2 current date. Here we discuss the basic syntax of a current date function and see different examples of the current date function. You may also have a look at the following articles to learn more –