EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Hour()
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL Hour()

By Aanchal SharmaAanchal Sharma

MySQL Hour()

Definition on MySQL Hour() Function

MySQL Hour() Function is MySQL DateTime function which is responsible to fetch the hour part from the datetime value specified in the query. The Hour() function in the server provides the hour portion extracted from the given date and time data type value from 0 to 838. Usually, MySQL supports the format ‘HH:MM:SS’ to query and show the time value which displays the time of any particular day that is within 24 hours. But for signifying a time interval concerning two events, the server applies the format as ‘HHH:MM:SS’ which is greater than 24 hours. Generally, in MySQL, the data type Time lies between -838:59:59 to 838:59:59.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The basic code for writing the syntax of MySQL Hour() function for querying the hour fragment from the given time value is defined as follows:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,768 ratings)

SELECT HOUR(Datetime);

Here, the command includes the SELECT keyword to display the value and the function parameter as Datetime data type for getting the hour value.

Also, you can consider the following syntax for the Hour() function in MySQL using the Time type as:

SELECT HOUR(Time);

Again, we can additionally apply other date and time-related data types as parameter for the Hour() function. Some of them are mentioned below:

SELECT HOUR(CURTIME());
SELECT HOUR(NOW());
SELECT HOUR(CURRENT_TIME());
SELECT HOUR(CURRENT_TIMESTAMP());

Hence, the MySQL Hour() function outputs the hour value with range 0 to 838 from the specified datetime value and time value can also be higher than 23 which is more than the day time value(0 to 23).

How Hour Function works in MySQL?

The MySQL HOUR() function is useful to extract hour portion from a datetime value in the server. This is a kind of MySQL time function which queries the hour from the standard time format as HH:MM:SS or sometimes HHH:MM:SS. The datetime format will have the syntax as YYYY-MM-DD HH:MM:SS.

This type of temporal function is used for manipulating the date-time value effectively in the MySQL database server when executed for any table column having datetime data type.

Let us write the syntax for stating a TIME column in the table as:

ColumnName TIME;

For example, while applying a query for a specific column name of a table supposing as Join_at name, we will edit the column name in the above syntax as:

Join_at TIME;

Also, we can evaluate some other temporal time function to extract the hour value such as current time function denoted as CURRENT_TIME() function, NOW() function and CURRENT_TIMESTAMP in MySQL. The CURRENT_TIME() function provides the present time value as result, NOW() function also returns the current date and time value of the server as well as CURRENT_TIMESTAMP also does the identical work providing the date and time values existing in the present.

Therefore, we can include the parameter values using any of these time functions in MySQL for the HOUR() and get the particular hour value as,

SELECT HOUR(datetime);

Taking a demonstration to show the working of this hour function in the server where we will enter the datetime value manually as below:

SELECT HOUR('2020-09-12 15:30:25');

Here, the output as hour value will be fragmented from the time format hour value which will be 15.

Also, when the time parameter holds the following value then also the hour function determines the hour value from it as,

SELECT HOUR('838:59:59');

As you can know the MySQL Hour() function will return the value as 838.

Examples

Let us illustrate the MySQL HOUR() function with few examples including simple and table column with date and time data type as follows:

Example #1 – Using the Hour() Function

  • For Time value as parameter:

SELECT HOUR('16:30:45');

Output:

MySQL Hour 1-1

  • For DateTime value as parameter:

SELECT HOUR('2020-09-10 16:30:45');

Output:

MySQL Hour 1-2

  • For CURRENT_TIME() function value as parameter:

SELECT HOUR(CURRENT_TIME());

  • For CURTIME() function value as parameter:

SELECT HOUR(CURTIME());

Output:

MySQL Hour 1-3

  • For CURRENT_TIMESTAMP value as parameter:

SELECT HOUR(CURRENT_TIMESTAMP);

Output:

MySQL Hour 1-4

  • For NOW() function value as parameter:

SELECT HOUR(NOW());

Output:

Number of rows

  • For Time value larger than 24 hours as parameter:

SELECT HOUR('305:30:45');

Output:

Number of rows 1

  • For negative Time value as parameter:

SELECT HOUR('-305:30:45');

Output:

Number of rows 2

Here, suppose there is any problem like if you need the resultant hour value as to be negative then, you can use the MySQL EXTRACT() function so find the output as we are looking for it:

SELECT EXTRACT(HOUR FROM '-305:30:45');

Output:

distract MySQL

  • For Date value and Time value with additional microseconds value as parameter:

SELECT HOUR('2020-09-10 05:30:45.000008');

Output:

Show hour

Here, the part of time value after decimal i.e. .000008 denotes the microseconds value.

Example #2 – Using the Table Column Datetime Value to Extract HOUR() Function

Let us take a sample table named Employee created in the database using the following query:

CREATE TABLE Employee(‘Person_ID INT PRIMARY KEY’, ‘Employee_Name VARCHAR(255) NOT NULL’, ‘Salary INT NOT NULL’, ‘JoinDate DATETIME NOT NULL’);

Also, we will add few records into the Employee table using the INSERT command:

INSERT INTO Employee(Peron_ID, Employee_Name, Salary, JoinDate) VALUES(‘101’,’Aakash’,’1045’, ‘2020-02-10 05:00:00’);

And so on.

Displaying the table contents as:

SELECT * FROM Employee;

Output:

Check all

Let us implement HOUR() function with the WHERE clause with the Employee table query as follows:

SELECT * FROM Employee WHERE HOUR(JoinDate) > 05;

Output:

join date 1

It outputs the rows from the table who joined after 5 A.M.

Also, let us query using the MySQL HOUR() function with the BETWEEN operator in the Employee table as follows:

SELECT * FROM Employee WHERE HOUR(JoinDate) BETWEEN 05 AND 10;

Output:

Join date

It results the data rows for a time duration of 5 to 10 hours.

Again, querying command using the MySQL HOUR() function with the IN operator in the Employee table as follows:

SELECT * FROM Employee WHERE HOUR(JoinDate) IN(2,5,10);

Output:

show rows 0-2

It gives information about join hour at 2 or 5 or 10 A.M. from the given datetime value in the JoinDate.

Conclusion

MySQL Hour() function is implemented in the server to find the hour component using the time value provided. Here, the result value will be in the range 0 to 23 for a time of day wossrth. However, this range of values for Time is much larger, therefore the Hour may result values which is greater than 23.

Recommended Articles

This is a guide to MySQL Hour(). Here we discuss the definition, How Hour Function works in MySQL? along with examples respectively. You may also have a look at the following articles to learn more –

  1. MySQL Repair Table
  2. MySQL DELETE JOIN
  3. MySQL User Permissions
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more