Converting date to character format in SQL

此生再无相见时 提交于 2019-12-13 07:19:57

问题


I'm trying to convert a date format stored m/d/yyyy to a 'yyyymmdd' character format, without the need to cast the date column every time I want to use the column in my query.

Currently I'm casting and formatting dates as chars: ((cast(cast(invitation_date as CHAR(8)) as date format 'YYYYMMDD')).

Is there a way to convert the data column once and call the converted character value later in the query?

I'm using Teradata in Aqua Data Studio 13.0.3.


回答1:


If source is a DECIMAL yyyymmdd you can do

CAST(invitation_date - 19000000 AS DATE) AS newcol

Teradata allows an alias to be used in any place, so you can simply do

WHERE newcol > DATE

Of course best case would be to change those columns to DATE during load.




回答2:


Navigate to File->Options -> Results Format -> Teradata. Then select the datatype Date and enter yyyyMMdd. Your result set will now return the specified date format. Let me know if this will solve your issue.




回答3:


select to_char(field, 'yyyy-mm--dd hh:mm:ss') from table; 



回答4:


You shouldn't put it like this, it simply won't work:

select invitationdate(field, 'yyyymmdd')

try this instead :

SELECT to_char(column_date, 'yyyy/mm/dd') from table;

as "man" said. please pay attention to the undescore (_) character; not this (-) one.




回答5:


Here is a longer but in a way simpler way that works

select yourdate ,cast((extract(year from yourdate)*100 + extract(month from yourdate))*100 + extract(day from yourdate) as char(8)) as yyyymmdd from ...



来源:https://stackoverflow.com/questions/21914444/converting-date-to-character-format-in-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!