SQL HOUR() Function


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

The SQL HOUR() function returns a integer number specifying a whole number between 0 and 23, inclusive, representing the hour of the day.

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

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


Search Keys

    The remote server returned an error: (404) Not Found.

Related Links

    The remote server returned an error: (404) Not Found.

SQL HOUR() Syntax

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

For SQL SERVER / MY SQL / MS ACCESS


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


SQL HOUR() Example - Using Expression Or Formula

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


SELECT 
NOW() AS 'Current Date and Time', 
HOUR(NOW()) AS 'Hour Value';

The result of above query is:

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

Sample Database Table - BookOrder

BookID BookName OrderDate Qty DeliveryDate
1 Art Of SQL 12-01-2010 18:24:46 2 20-01-2010 07:47:23
2 Programming Microsoft sql Server 2012 12-03-1998 21:51:34 1 15-03-1998 07:52:29
3 SQL Puzzles & Answers 24-07-2000 07:04:50 19 26-07-2000 12:42:40
4 Oracle Plsql Programming Fundamentals 19-09-2005 18:58:39 6 22-10-2005 06:17:45
5 Transact SQL Cookbook 25-05-2003 13:18:12 12 28-06-2003 17:33:27
6 The Gurus Guide To SQL Server 14-04-1997 21:09:35 7 26-05-1997 19:35:40
7 The Complete Guide to Oracle 26-01-2008 02:02:18 16 28-02-2008 11:18:46
8 SQL Server Database Internals 20-01-2002 05:26:11 8 28-01-2002 14:21:33
9 Programming With Oracle 26-07-2003 14:54:44 19 28-08-2003 11:35:11
10 SQL Visual Quickstart 17-08-2002 20:49:25 4 24-08-2002 04:30:39
11 The Complete Guide to MySql 26-04-2003 16:45:08 11 28-05-2003 10:03:56
12 Oracle Cookbook 11-10-2008 20:59:39 11 18-11-2008 13:08:30

SQL HOUR() Example - With Table Column

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


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

The result of above query is:

OrderDate Order HOUR DeliveryDate Delivery HOUR
12-01-2010 18:24:46 18 20-01-2010 07:47:23 7
12-03-1998 21:51:34 21 15-03-1998 07:52:29 7
24-07-2000 07:04:50 7 26-07-2000 12:42:40 12
19-09-2005 18:58:39 18 22-10-2005 06:17:45 6
25-05-2003 13:18:12 13 28-06-2003 17:33:27 17
14-04-1997 21:09:35 21 26-05-1997 19:35:40 19
26-01-2008 02:02:18 2 28-02-2008 11:18:46 11
20-01-2002 05:26:11 5 28-01-2002 14:21:33 14
26-07-2003 14:54:44 14 28-08-2003 11:35:11 11
17-08-2002 20:49:25 20 24-08-2002 04:30:39 4
26-04-2003 16:45:08 16 28-05-2003 10:03:56 10
11-10-2008 20:59:39 20 18-11-2008 13:08:30 13

Search Keys

    The remote server returned an error: (404) Not Found.

Related Links

    The remote server returned an error: (404) Not Found.