Search code examples
sqlpostgresqlconcatenationgenerate-series

psql generate_series method doesn't support concat inside


The following sql code worked as expected

generate_series('2018-06-29 00:00:00','2018-06-29 23:00:00', interval '1 hour')

but when I put concat method instead of first 2 parameters it's rise an error message

generate_series(concat('2018-06-29 00:00:00', '+05'), concat('2018-06-29 23:00:00', '+05'), interval '1 hour')

The error message

function generate_series(text, text, interval) does not exist

Solution

  • If you concat it becomes as text data type. Hence you cannot generate series.

    Below query will produce desired result

    No need to write "interval". Since start and end are timestamp postgresql understands 5h and 1h are 5hours and 1hour

     select 
     generate_series(timestamp '2018-06-29 00:00:00' + '5h',
                     timestamp '2018-06-29 23:00:00' + '5h', 
                    '1h')