Calendar Date/Time FunctionsThe calendar date/time functions manipulate data in data types for date, time and timestamp. (The timestamp data type is a combination of date and time.) Current_DateReturns the current date. The date is determined by the computer. The value does not contain a time component. Syntax: CURRENT_DATE Current_TimeReturns the current time. The time is determined by the computer The value does not contain a date component. Note: This function obtains the current time at the time the report is run. Using this function with an analytics subject area prevents the report from caching, which decreases performance. Syntax: CURRENT_TIME (n) where:
Current_TimeStampReturns the current date/timestamp. The timestamp is determined by the computer. Note: This function obtains the current time at the time the report is run. Using this function with an analytics subject area prevents the report from caching, which decreases performance. Syntax: CURRENT_TIMESTAMP (n) where:
Day_Of_QuarterReturns a number (between 1 and 92) corresponding to the day of the quarter for the specified date. Syntax: DAY_OF_QUARTER (date_expression) where:
DayNameReturns the name of the day of the week (in English) for a specified date. Syntax: DAYNAME (date_expression) where:
DayOfMonthReturns the number corresponding to the day of the month for a specified date. Syntax: DAYOFMONTH (date_expression) where:
DayOfWeekReturns a number between 1 and 7, corresponding to the day of the week, for a specified date. The number 1 corresponds to Sunday, and the number 7 corresponds to Saturday. Syntax: DAYOFWEEK (date_expression) where:
DayOfYearReturns the number (between 1 and 366) corresponding to the day of the year for a specified date. Syntax: DAYOFYEAR (date_expression) where:
HourReturns a number (between 0 and 23) corresponding to the hour for a specified time. For example, 0 corresponds to 12 a.m. and 23 corresponds to 11 p.m. Syntax: HOUR (time_expression) where:
MinuteReturns a number (between 0 and 59) corresponding to the minute for a specified time. Syntax: MINUTE (time_expression) where:
MonthReturns the number (between 1 and 12) corresponding to the month for a specified date. Syntax: MONTH (date_expression) where:
Month_Of_QuarterReturns the number (between 1 and 3) corresponding to the month in the quarter for a specified date. Syntax: MONTH_OF_QUARTER (date_expression) where:
MonthNameReturns the name of the month (in English) for a specified date. Syntax: MONTHNAME (date_expression) where:
NowReturns the current timestamp. The NOW function is equivalent to the CURRENT_TIMESTAMP function. Note: This function obtains the current time at the time the report is run. Using this function with an analytics subject area prevents the report from caching, which decreases performance. Syntax: NOW () Quarter_Of_YearReturns the number (between 1 and 4) corresponding to the quarter of the year for a specified date. Syntax: QUARTER_OF_YEAR (date_expression) where:
SecondReturns the number (between 0 and 59) corresponding to the seconds for a specified time. Syntax: SECOND (time_expression) where:
TimestampAddThe TimestampAdd function adds a specified number of intervals to a specified timestamp. A single timestamp is returned. Syntax: TimestampAdd (interval, integer_expression, timestamp_expression) where:
A null integer-expression or a null timestamp-expression passed to this function will result in a null return value. In the simplest scenario, this function merely adds the specified integer value (integer_expression) to the appropriate component of the timestamp, based on the interval. Adding a week translates to adding seven days, and adding a quarter translates to adding three months. A negative integer value results in a subtraction (going back in time). An overflow of the specified component (such as more than 60 seconds, 24 hours, twelve months, and so on) necessitates adding an appropriate amount to the next component. For example, when adding to the day component of a timestamp, this function considers overflow and takes into account the number of days in a particular month (including leap years when February has 29 days). When adding to the month component of a timestamp, this function verifies that the resulting timestamp has a sufficient number of days for the day component. For example, adding 1 month to 2000-05-31 does not result in 2000-06-31 because June does not have 31 days. This function reduces the day component to the last day of the month, 2000-06-30 in this example. A similar issue arises when adding to the year component of a timestamp having a month component of February and a day component of 29 (that is, last day of February in a leap year). If the resulting timestamp does not fall on a leap year, the function reduces the day component to 28. The following are examples of the TimestampAdd function: The following code example asks for the resulting timestamp when 3 days are added to 2000-02-27 14:30:00. Because February, 2000 is a leap year, this returns a single timestamp of 2000-03-01 14:30:00. TimestampAdd(SQL_TSI_DAY, 3, TIMESTAMP‘2000-02-27 14:30:00’) The following code example asks for the resulting timestamp when 7 months are added to 1999-07-31 0:0:0. This returns a single timestamp of 2000-02-29 00:00:00. Notice the reduction of day component to 29 because of the shorter month of February. TimestampAdd(SQL_TSI_MONTH, 7, TIMESTAMP‘1999-07-31 00:00:00’) The following code example asks for the resulting timestamp when 25 minutes are added to 2000-07-31 23:35:00. This returns a single timestamp of 2000-08-01 00:00:00. Notice the propagation of overflow through the month component. TimestampAdd(SQL_TSI_MINUTE, 25, TIMESTAMP‘2000-07-31 23:35:00’) TimeStampDiffThe TimestampDiff function returns the total number of specified intervals between two timestamps. Syntax: TimestampDiff (interval, timestamp_expression1, timestamp_expression2) where:
A null timestamp-expression parameter passed to this function will result in a null return value. This function first determines the timestamp component that corresponds to the specified interval parameter. For example, SQL_TSI_DAY corresponds to the day component and SQL_TSI_MONTH corresponds to the month component. The function then looks at the higher order components of both timestamps to calculate the total number of intervals for each timestamp. For example, if the specified interval corresponds to the month component, the function calculates the total number of months for each timestamp by adding the month component and twelve times the year component. Finally, the function subtracts the first timestamp’s total number of intervals from the second timestamp’s total number of intervals. When calculating the difference in days, the function truncates the time values from both Timestamp expressions and then subtracts the date values. When calculating the difference in weeks, the function calculates the difference in days and divides by seven before rounding. When calculating the difference in quarters, the function calculates the difference in months and divides by three before rounding. When calculating the difference in years, the function calculates the difference in months and divides by twelve before rounding. TimestampDiff Function and Results ExampleThe following code example asks for a difference in days between timestamps 1998-07-31 23:35:00 and 2000-04-01 14:24:00. It returns a value of 610. Notice that the leap year in 2000 results in an additional day. TimestampDIFF(SQL_TSI_DAY, TIMESTAMP‘1998-07-31 23:35:00’, TIMESTAMP‘2000-04-01 14:24:00’) Week_Of_QuarterReturns a number (between 1 and 13) corresponding to the week of the quarter for the specified date. Syntax: WEEK_OF_QUARTER (date_expression) where:
Week_Of_YearReturns a number (between 1 and 53) corresponding to the week of the year for the specified date. Syntax: WEEK_OF_YEAR (date_expression) where:
YearReturns the year for the specified date. Syntax: YEAR (date_expression) where:
Example: YEAR (CURRENT_DATE) |
Published 6/21/2021 | Copyright © 2005, 2021, Oracle and/or its affiliates. Legal Notices. |