How to use like operator for timestamp in PostgreSQL?

Member

by daisha , in category: PHP Databases , a year ago

How to use like operator for timestamp in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 9 months ago

@daisha 

You can use the LIKE operator to match a string pattern in a timestamp column in PostgreSQL. Here is an example:

1
2
SELECT * FROM table_name
WHERE timestamp_column LIKE '2022-12-25%';


This will return all rows from the table_name table where the timestamp_column starts with '2022-12-25'. The % character is used as a wildcard to match any characters after the specified string.


You can also use the ILIKE operator, which is case-insensitive, instead of LIKE if you don't want the search to be case-sensitive.

1
2
SELECT * FROM table_name
WHERE timestamp_column ILIKE '2022-12-25%';


Note that the LIKE and ILIKE operators can only be used with string data types, so if your timestamp column is of a different data type, you will need to cast it to a string before using these operators. For example:

1
2
SELECT * FROM table_name
WHERE CAST(timestamp_column AS TEXT) LIKE '2022-12-25%';