7

I'm trying to cast or convert a timestamp column to an integer value in a Redshift database. I know it is possible in other SQL-based databases and I've done this before, however in Redshift I can't get it to work.

I've tried some variations of cast and also convert (http://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html).

Here is an example:

SELECT cast(finish_time as integer) FROM table;

It gives me the error message:

SQL Error Invalid operation: cannot cast type timestamp without time zone to integer;

Is it possible to get a timestamp as an integer?

1 Answer 1

11

try select extract('epoch' from finish_time), it will give you Unix ms timestamp

3
  • Thanks, this is what I was looking for. Commented Jul 31, 2017 at 15:09
  • @AlexYes However i am not able to use this epoch timestamp in order by clause to sort the rows. Any help in that.
    – jeevs
    Commented Sep 3, 2019 at 9:16
  • @jeevs please ask a question with the code sample and amazon-redshift tag
    – AlexYes
    Commented Sep 3, 2019 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.