// dates & times reference

SQL TIME() function

Returns just the clock portion, the standard trick for hour-of-day histograms and time-window comparisons across days.

Category
Dates & times
Returns
time
In this category
50 of 121

Syntax

TIME(ts)  -- MySQL, SQLite
CAST(ts AS time)  -- SQL Server
ts::time  -- PostgreSQL

Each block is a separate form. The support table and the examples show which engine takes which.

Engine support

EngineSupportNotes
SQL Server Different name or syntax CAST(ts AS time)
PostgreSQL Different name or syntax ts::time; time(ts) is a syntax error
MySQL Yes Supported under this name.
SQLite Yes Supported under this name.
Oracle Different name or syntax no TIME type: TO_CHAR(d, 'HH24:MI:SS'), or d - TRUNC(d) as a fraction of a day

Examples

MySQL

SELECT TIME(created_at) AS clock FROM orders;

PostgreSQL

SELECT (created_at AT TIME ZONE 'UTC')::time AS clock FROM orders;

SQLite

SELECT time(created_at) AS clock FROM orders;

Good to know

  • Oracle has no TIME type. d - TRUNC(d) on a DATE is a NUMBER, the fraction of the day (0.5 is noon); TO_CHAR(d, 'HH24:MI:SS') renders the clock as text.
  • PostgreSQL's ::time renders the timestamptz in the session zone first; pin the zone with AT TIME ZONE before casting for stable results.
  • Comparing TIME across DST days has a hidden trap: wall clocks repeat an hour in autumn, so two real events share one time value.

Questions about TIME()

How do I filter for events after 17:00?

MySQL: TIME(ts) > '17:00:00'. PostgreSQL: ts::time > '17:00' or EXTRACT(hour FROM ts) >= 17. Neither can use a plain index on ts; an expression index on the time part can.

What does Oracle return for the time part?

Not a TIME type. d - TRUNC(d) on a DATE is a number of days (0.75 is 18:00); TO_CHAR(d, 'HH24:MI:SS') is the text route.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.