4

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
1
  • You say you've failed with that code, what is the failure message you're getting? Commented Aug 28, 2018 at 17:28

2 Answers 2

4

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.

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

1 Comment

This answer worked perfectly for what I was trying to do.
0

I think what you want is:

SELECT '1/1/2018' AS TIMESTAMP;

  timestamp 
 -----------
  1/1/2018

Or

SELECT CAST('1/1/2018' AS TIMESTAMP); 

      timestamp      
 ---------------------
  2018-01-01 00:00:00

Or

SELECT CAST('1/1/2018' AS DATE);

    date    
------------
 2018-01-01

It depends really on what you want to do.

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.