Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query date field of json object in PostgreSQL 9.3?

imagine a PostgreSQL 9.3 with a table that has an sql column that contains an object:

{"start":"2016-02-04 00:45:56.000"}

How could I query it to get objects with start date > :"2016-01-01 00:00:00.000"?

This question is specificly for version 9.3

like image 583
BanzaiTokyo Avatar asked Sep 14 '16 16:09

BanzaiTokyo


1 Answers

Something like this should do it:

SELECT *
FROM mytable
WHERE (c->>'start')::timestamp with time zone > '2016-01-01'::timestamp with time zone;

I assume that the table is called mytable and the JSON column is called c. This also assumes that the times are in UTC. If your time is in local time, use timestamp without time zone instead.

like image 124
redneb Avatar answered Nov 09 '22 10:11

redneb