How to query by time in postgresql?

Member

by samara , in category: MySQL , 3 months ago

How to query by time in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 3 months ago

@samara 

To query by time in PostgreSQL, you can use the EXTRACT function to extract the hour, minute, or second from a timestamp or time data type column. Here are a few examples of how you can query by time in PostgreSQL:

  1. To select records where the time is after a certain hour:
1
SELECT * FROM table_name WHERE EXTRACT(HOUR FROM time_column) > 12;


  1. To select records where the time is between a certain range of hours:
1
SELECT * FROM table_name WHERE EXTRACT(HOUR FROM time_column) BETWEEN 9 AND 17;


  1. To select records where the time is before a certain hour:
1
SELECT * FROM table_name WHERE EXTRACT(HOUR FROM time_column) < 9;


You can also extract minutes or seconds using the MINUTE and SECOND keywords in the EXTRACT function. Remember to replace table_name and time_column with your actual table and column names.