SQL SECOND() Function

SQL SECOND() Function


The SQL SECOND() is a function, and return or extract the second portion of a given input time value.

The SQL SECOND() function returns a integer number specifying a whole number between 0 and 59, inclusive, representing the second of the SECONDs.

The SQL SECOND() function is supports only datetime or timestamp based table columns or fields.

It can be used in SELECT statement as well in where clause.


SQL SECOND() Syntax

The below syntax is used to get second of the SECOND value (a integer number between 0 to 59, inclusive) from a given input datetime value.

For SQL SERVER / MY SQL / MS ACCESS


SELECT SECOND('valid_datetime_value' or 'valid_timestamp_value');




SQL SECOND() Example - Using Expression Or Formula

The following SQL SELECT statement will extract the second of the SECOND value from a given input datetime value. We use now() function to get the input date value.


SELECT 
NOW() AS 'Current Date and Time', 
SECOND(NOW()) AS 'Second Value';

The result of above query is:

Current Date and Time Second Value
24-08-2012 19:37:30 30

Sample Database Table - BookOrder

BookID BookName OrderDate Qty DeliveryDate
1 The Database Language SQL 16-06-2009 13:16:48 9 20-07-2009 23:21:37
2 Pro Oracle SQL 12-07-2007 01:24:50 7 26-07-2007 04:09:30
3 A Visual Introduction To Sql 11-02-2004 02:02:31 1 17-02-2004 14:17:10
4 Access 2010 Pure Sql 20-05-2010 05:50:19 2 28-05-2010 08:35:55




SQL SECOND() Example - With Table Column

The following SQL SELECT statement display the column "OrderDate" and "DeliveryDate" from the "BookOrder" table. The SECOND() function will extract the SECOND value(a whole integer number from 1 to 60) from the column "OrderDate" and we will stored that value to a new column called "Order SECOND" and "Delivery SECOND".


SELECT 
OrderDate, SECOND(OrderDate) As 'Order SECOND', 
DeliveryDate, SECOND(DeliveryDate) As 'Delivery SECOND' 
FROM BookOrder;

The result of above query is:

OrderDate Order SECOND DeliveryDate Delivery SECOND
16-06-2009 13:16:48 48 20-07-2009 23:21:37 37
12-07-2007 01:24:50 50 26-07-2007 04:09:30 30
11-02-2004 02:02:31 31 17-02-2004 14:17:10 10
20-05-2010 05:50:19 19 28-05-2010 08:35:55 55