HOUR_SECOND
HOUR_SECOND is a temporal data type in SQL, which represents a duration of time in terms of hours and seconds. It is used in interval expressions, specifically to add or subtract a specified number of hours and seconds from a time or datetime value. The format is expressed as 'HH:SS'.
Example
SELECT CURTIME() as 'Current Time', DATE_FORMAT(CURTIME(),'%H:%i:%s') as 'Hour:Minute:Second';
Output
| Current Time | Hour:Minute:Second || ------------ | ------------------ || 12:34:56 | 12:34:56 |
Explanation
In this example, the CURTIME()
function is used to get the current time. The DATE_FORMAT()
function formats the date as hours (H), minutes (i), and seconds (s). The result is a table that displays the default format of CURTIME()
and the formatted time.
Example
SELECT CONVERT(VARCHAR, GETDATE(), 108) AS Current_Time
Output
Current_Time------------14:30:23
Explanation
In this SQL query, the function GETDATE() returns the current system date and time in SQL Server. The CONVERT function then formats this datetime value into a string that only includes the hours, minutes, and seconds (HH:MM:SS).
Oracle SQL does not support ‘HOUR_SECOND’ as it is a date format in MySQL. However, you can use extract
function in Oracle to get the hour and second from a timestamp.
Example
SELECT EXTRACT(HOUR FROM timestamp) AS HOUR, EXTRACT(SECOND FROM timestamp) AS SECONDFROM ( SELECT TO_TIMESTAMP('2022-12-25 10:21:33', 'YYYY-MM-DD HH24:MI:SS') AS timestamp FROM dual);
Output
HOUR SECOND----- ------10 33
Explanation
In the above example, EXTRACT
function is being used to fetch the hour and second from a given timestamp. The TO_TIMESTAMP
function is used to convert a string into a timestamp.