Explain Codes LogoExplain Codes Logo

Time part of a DateTime Field in SQL

sql
date-time
sql-functions
time-extraction
Anton ShumikhinbyAnton Shumikhin·Aug 24, 2024
TLDR

In SQL Server, extract the time component from a DateTime like so:

SELECT CAST(YourDateTimeField AS TIME) AS TimeOnly FROM YourTable; -- Wham! Bam! Just the time, ma'am.

And for MySQL, apply the TIME function:

SELECT TIME(YourDateTimeField) FROM YourTable; -- Quick n' slick! All you need is TIME.

Time extraction in SQL: the know-hows

Convert and compare: SQL Server style

SQL Server offers an armory of functions for DateTime handling. To extract the time part, the CONVERT function is your buddy:

SELECT CONVERT(VARCHAR(8), YourDateTimeField, 108) AS TimeOnly FROM YourTable; -- Note format code 108 for HH:MM:SS

To access records at a specific time, manipulate the DateTime field like this:

SELECT YourDateTimeField FROM YourTable WHERE CONVERT(VARCHAR(5), YourDateTimeField, 108) = '17:00'; -- It's 5 o'clock somewhere!

DATEPART and CAST: your time-travelling tools

Date and time functions like DATEPART and CAST are your Tardis for filtering DateTime fields. Here's how:

SELECT YourDateTimeField FROM YourTable WHERE DATEPART(hour, YourDateTimeField) = 17; -- Coffee break at 5 PM!

Use CAST to handle time comparisons in later SQL Server versions:

SELECT YourDateTimeField FROM YourTable WHERE CAST(YourDateTimeField AS TIME) >= '17:00:00'; -- Sorry, I only work after 5.

Date math: beating time at its own game

Using DATEADD and DATEDIFF allows smooth sailing between the often choppy waters of time operations in SQL:

SELECT DATEADD(day, DATEDIFF(day, 0, YourDateTimeField), 0) FROM YourTable; -- Here we reset the day part to midnight, so tomorrow never dies.

Adjusting your DateTime to a specific time is just as breezy:

SELECT DATEADD(hour, 17, DATEADD(day, DATEDIFF(day, 0, YourDateTimeField), 0)) FROM YourTable; -- Strips date down to midnight, then we ADD 17 hours - all dressed up for 5 PM!

Practical guide to powerful SQL time queries

Boosting your SQL time chiropractics with variables

Elevator pitch for the use of Variables in SQL:

DECLARE @myDateTime DATETIME = '2023-04-14 17:00:00'; -- Meet my variable @myDateTime, a rather charming entity. SELECT @myDateTime - CAST(@myDateTime AS DATE) AS TimeOnly; -- Sorry date, it's not you, it's me... Time.

Couple of solid pointers when dealing with times:

  • Verify the hour and minute are two digits each. Failing which, use RIGHT('0' + CAST(... AS VARCHAR), 2) to ensure compliance.
  • Mind the time zone of your system. It's a UTC vs. local time zones showdown.
  • The golden rule: stick to ISO-compliant formats when cross-databasing with SQL.

Walking with different SQL dialects

Both PostgreSQL and Oracle have distinct time extraction ambitions:

  • PostgreSQL's EXTRACT(HOUR FROM YourDateTimeField) aims for granular time part retrieval.
  • Oracle's TO_CHAR(YourDateTimeField, 'HH24:MI:SS') pursues string representation of time.

Live SQL action with SQLFiddle!

Playing around with live SQL queries is a great learning exercise. Marvel at how CAST and CONVERT materialise your desired output in real-time on SQLFiddle. You can also practise various SQL time operations until you're a certified SQL time-lord!