I am trying to export from SQL to .csv and it works if I hard code it to accept a certain number of arguments. The things is, I want to allow the user to request any number of arguments and have these be passed to the where clause. The code should make this a bit more clear.
create temporary table bdates as
select tt.date, tt.time, tt.location
from birthdays as bd
inner join days as d
on (d.id = bd.birth_id)
inner join total_time as tt
on (bd.date = tt.date and
bd.time = tt.time and
d.day_of = tt.location)
where tt.date in(:date1, :date2) --defined by user at command line
order by...
\copy bdates to '.csv'
So what I think I want to do is pass a list to that where clause instead of explicit :dates# variables. For example, a person could run the script with the argument '2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00', or just two arguments or one. In the case of three the string would be parsed to '2012-01-04 12:00', '2012-02-04 12:00', '2012-03-04 12:00'.
I've tried string_to_array(), unnest(regexp_matches(:dates, expression)) and regexp_split_to_table(:dates, expression), though I'm not sure how to do the join. the various solutions I've tried have produced numerous errors including:
cannot cast type text[] to timestamp without time zone
cannot cast type record to timestamp without time zone
regexp_split does not support the global option
argument of WHERE must not return a set
The last one is especially disheartening and I'm at a loss and would appreciate any input. There's a simpler way to do this, isn't there? Thanks!