Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSTGRES datetime2 to timestamp

Tags:

postgresql

I am migrating to postgres from sql server. Is there a way that I could do the following and have it still work? SELECT CAST('1/1/2018' AS datetime2) I have tried and failed with

   CREATE TYPE datetime2 AS (date2 TIMESTAMP); --doesn't work
like image 349
Daniel L. VanDenBosch Avatar asked Oct 19 '25 13:10

Daniel L. VanDenBosch


1 Answers

If you want to have datetime2 as a synonym for timestamp type in PostgreSQL then use domain instead of type:

create domain datetime2 as timestamp;

Then your cast will work as is:

SELECT CAST('1/1/2018' AS datetime2);
┌─────────────────────┐
│      datetime2      │
├─────────────────────┤
│ 2018-01-01 00:00:00 │
└─────────────────────┘

Except the name it will have same behavior as original timestamp type.

What you are doing in your question is to create composite type (record with single field date2 of timestamp type) which could be also highly useful but for another kinds of tasks.

like image 175
Abelisto Avatar answered Oct 21 '25 11:10

Abelisto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!