SECOND
SECOND is a SQL function used to extract the second from a given time or datetime expression. The function returns an integer value representing the second component of the provided time, ranging from 0 to 59.
SECOND(time)
- time: This is the time or datetime value from which you want to extract the second. It can be in ‘HH
Example
SELECT SECOND('2020-05-12 22:34:57');
Output
57
Explanation
The SECOND
function in this example returns the seconds from the time provided. In the string ‘2020-05-12 22:34:57’, the seconds are ‘57’, hence the output is 57.
SECOND(time)
- time: This is the time value from which the “second” part will be extracted. It accepts a valid time string or time object. The string format can be ‘HH
Example
SELECT EXTRACT(SECOND FROM TIMESTAMP '2022-12-31 13:14:15.926');
Output
15.926
Explanation
The query above extracts and returns the seconds component from the provided timestamp, which includes the fractional part up to milliseconds.
DATEPART(SECOND, date)
- second: The date part argument that specifies the component of the date from which to extract the integer value. In this case, SECOND extracts the seconds from a specified date.
- date: The date argument that specifies the date from which the function extracts the second. This must be a valid expression returning a date or datetime value.
Example
SELECT SECOND('2022-03-03T14:53:20') AS 'Second';
Output
'20'
Explanation
In this example, the SQL SECOND
function is used to extract the second part from the given time. The output is 20
, which is the seconds portion of the provided timestamp.
SECOND(date)Note: The ‘date’ parameter represents the date from which the ‘second’ portion is to be extracted.
- date: The input date from which the ‘second’ portion is to be extracted.
Example
SELECT SECOND(TO_DATE('2022-11-23 12:34:56', 'YYYY-MM-DD HH24:MI:SS')) AS EXTRACTED_SECOND FROM dual;
Output
56
Explanation
The SECOND
function is used to extract the second from a specific date and time. In this case, the second ‘56’ was extracted from the date ‘2022-11-23 12:34:56’.