34

I've inherited a database with a quirky schema. Without boring of the details I have an odd use case.

The table contains a filed completed_at that is a timestamp without timezone. What I'm trying to do is order the results by the timestamp where the timestamp is > X

The kicker is X is a numeric (or decimal) value. Trying to convert the timestamp to int is failing or even to string.

If you simply try completed_at > 0 you get

2
  • 9
    extract(epoch from timestamp_column)? Commented Jan 26, 2015 at 17:47
  • Does the integer represent "epoch" time, or is it an embedded timestamp like 201501010800 (meaning 2015-01-01 08:00)? Commented Jan 26, 2015 at 18:52

4 Answers 4

45

The only answer I found was in this message: https://www.postgresql.org/message-id/3EE8B4ED.1060200%40pgsqldb.com

Basically, I guess, the answer is: select cast(extract(epoch from current_timestamp) as integer);

Sign up to request clarification or add additional context in comments.

Comments

7

I am using the below code to convert the current time to Bigint.

SELECT cast(to_char((current_timestamp)::TIMESTAMP,'yyyymmddhhmiss') as BigInt)

Comments

0

I had a similar problem: I wanted to extract the year as an Integer, so using the above I wrote CAST(EXTRACT(YEAR FROM CURRENT_TIMESTAMP) AS INTEGER)into my PostgreSQL database and then examined the constraint in PGADMIN 4 and it came up with the following alternative, which I found interesting:

date_part('year'::text, CURRENT_TIMESTAMP)::integer

Comments

-13

I would use the following code if it is SQL. It works for me. Change the ASC to DESC if it is the wrong way. You should not need to convert anything.

ORDER BY `timestamp_column` ASC

1 Comment

Backticks ` won't work, that's a MySQL-only thing. Don't use it, you never need it. If you really really want something around your database objects, use double quotes ", that's a standard in SQL and works in most databases. Including MySQL.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.