0

I am trying to migrate a column in a table from timestamp (double precision) to a Date.

For example, right now seen_timestamp column contains values like this one:

1643302746243

Values now are all UTC. So that unix timestamp would be:

Thu, 10 Mar 54044 17:04:03 GMT

Which is part of the mistake I made. The timestamp is supposed to be this:

1643302746.243

Which would be this date:

01/27/2022, 04:59:06 PM

So, I could first update all values by dividing by 1000, and then migrating over to UTC Date type....

I tried this:

ALTER TABLE car
ALTER COLUMN seen_timestamp TYPE DATE USING seen_timestamp::DATE;

I get the following error:

cannot cast type double precision to date

Makes sense. I just don't know how to change/migrate the column to Date type.

How can I make this work?

7
  • 2
    double precision is not a "timestamp" to begin with. A timestamp is a timestamp. But to answer your question we would need to know what values does the column contain? Commented Jan 28, 2022 at 14:58
  • yes, you're right. I'll update the question. Commented Jan 28, 2022 at 14:59
  • 2
    Are you sure you want a timestamp from the year 54044? Commented Jan 28, 2022 at 15:06
  • hahaha. I edited the question. Sorry. Commented Jan 28, 2022 at 15:12
  • 2
    1) Create a new empty column of type TIMESTAMP with a different name. 2) Update the new column converting the number to a timestamp. 3) Drop the existing BAD column. 4) Rename the new column to have the name of the old column. Commented Jan 28, 2022 at 15:14

3 Answers 3

4

Use the appropriate USING clause:

ALTER TABLE car
ALTER COLUMN seen_timestamp TYPE timestamp
USING to_timestamp(seen_timestamp / 1000.0::numeric);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, silly me, I declared the column as date rather than as timestamp. Fixed. You were right all along.
1

So I got it working. Both comments from The Impaler and answer from Laurenz helped get in the right direction. This is what works for me:

-- add new tmp column to store timestamps
ALTER TABLE car ADD COLUMN tmp_seen_timestamp timestamptz;
-- update new column using seen_timestamp values/1000
UPDATE car SET tmp_seen_timestamp=to_timestamp(seen_timestamp / 1000.0::numeric) where seen_timestamp is not null;
-- remove original column
ALTER TABLE car drop column seen_timestamp;
-- raname column...
ALTER TABLE car rename column tmp_seen_timestamp to seen_timestamp;

This works too, following what Laurenz posted:

ALTER TABLE car
ALTER COLUMN seen_timestamp TYPE timestamptz
USING to_timestamp(seen_timestamp / 1000.0::numeric);

This does what it needs, but, I am confused why using 'date' as type would not provide full precision.

2 Comments

Check out the docs for Date/Time data types. The date, timestamp, and timestamptz types consume 4, 8, and 12 bytes, respectively. One reason to use date would be to save space. Or maybe the application would never need resolution down to hours/minutes/seconds... and it would be extra work every time to cast a high resolution type (date+time) to a lower resolution type (date only). It's also worth noting that there are time only data types, too. Some DB designers might wish to have date and time in separate columns.
Good info. Thanks!
-1

How to update timestamp datatype in postgresql

ALTER TABLE [TABLE_NAME] ALTER COLUMN [COLUMN_NAME] TYPE TIMESTAMP without time zone

Comments

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.