INTERVAL
INTERVAL is a SQL keyword that signifies a specific time span. It's used in various SQL functions and operations for manipulating and calculating dates and times. This data type can represent a period ranging from single fractional seconds up to a period of time consisting of years and months.
Example
Output
Explanation
The SQL statement above adds seven days to the date ‘2021-04-01’. The INTERVAL keyword is used to specify the period which is to be added, in this case, 7 DAYS.
Example
Output
Explanation
In this example, INTERVAL '3 hours'
adds 3 hours to the given time 02:00:00
. The output 05:00:00
is the resultant time after adding the interval.
Example
Output
Explanation
The INTERVAL keyword is not used directly in SQL Server. However, similar functionality where we can increase or decrease a date/time by a certain interval is achieved using the DATEADD function.
In this example, 20 minutes is subtracted from the specified time ‘20:00:00’ using the DATEADD function, which represents an interval of -20 minutes. The result is ‘19:40:00’.
Example
Output
Explanation
In the above example, the INTERVAL
keyword is used to define a time interval of 300 seconds. The SQL query selects this interval from the Oracle dual
table, and the output is formatted as an interval of 5 minutes (equivalent to 300 seconds).