Conversion of timestamp to date type mmddyy fails

1

I need to convert timestamp into MMDDYY format : 2020-01-31 01:14:00.000 must be converted to 01/31/20 the timestamp is stored in a table and I need to display it as MMDDYY Here is what I am trying to use : to_date(cast(timestamp_column as varchar), 'DDMMYY') - returns a completely different date

Please anyone help me out here asap

akc_adi
질문됨 일 년 전2275회 조회
2개 답변
2

to_date(cast(timestamp_column as varchar), 'DDMMYY')

So breaking this down, start at the inner most function.

cast(timestamp_column as varchar)

This will convert the timestamp to varchar and be something like this 2022-11-09 15:22:48.594634+00

So then you have to_date() with the mask of DDMMYY. This means you are telling the to_date function that the format of the first parameter will be formatted like 091122 when it isn't at all. I'm guessing the output was something like 1990-10-22.

How it is stored versus how it is displayed are two different things. It is a timestamp so don't worry about how it is stored. It isn't a string. So to display the timestamp in the format you want, use to_char. It means you are taking the timestamp and formatting with the mask you are providing.

to_char(timestamp_column, 'DDMMYY')

For example:

select to_char(now(), 'DDMMYY');
 to_char 
---------
 091122
profile pictureAWS
전문가
답변함 일 년 전
AWS
전문가
검토됨 일 년 전
0

Hi akc_adi,

Have you tried the following?

select to_char(timestamp_column,'DDMMYY')

If the timestamp_column is stored as varchar, then you have to cast to timestamp before:

select to_char(cast('2020-01-31 01:14:00.000' as timestamp),'DDMMYY')

Thanks,

AWS
전문가
Ziad
답변함 일 년 전
  • the additional explanation in the other answer make it easier to understand.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠