postgreSql搜索返回日期数组

时间:2018-01-24 06:58:10

标签: sql postgresql date

我有一个Text [] colom,它以dd-mm-yyyy格式存储BillDate

Id  BillDate text[]
1   20-01-2018
2   10-01-2018, 12-01-2018
3   19-12-2017,13-07-2017,20-01-2018
4   25-12-2017, 18-12-2017

我的PostgreSQL需要从&过滤账单日期范围为:12-01-2018 to 21-01-2018,结果应为ID:1,2,3,因为BillDate中至少有一个值存在于范围

之内

更改为MyQuery:

var datefrom = ['12-01-2018'];
var dateto = ['21-01-2018'];

' SELECT "Id", "BillDate" FROM "Table" '+
' where "BillDate" between $1 and $2 ',[datefrom,dateto]

但我没有得到例外,因为postgres以yyyy-dd-mm格式检查日期

1 个答案:

答案 0 :(得分:1)

首先,要求Postgres了解您的日期格式,以便能够投射它:

integers

现在,从范围内的数组中获取至少有一个日期的ID:

t=# set datestyle to DMY;
SET

现在,如果您想要id和billdate,请将结果与t=# with w as ( select id,unnest(bd)::date bd from w ) select id from w where bd between '12-01-2018' and '21-01-2018' ; id ---- 1 2 3 (3 rows)

上的原始表相关联