[英]Convert String to Date in CASE expression Oracle SQL
I'm using the following CASE
expression, but I want the field to be a Date and not string format:我正在使用以下
CASE
表达式,但我希望该字段是日期而不是字符串格式:
CASE WHEN TO_CHAR(DATE, 'MM/DD/YYYY') = '01/01/1800' THEN '01/01/2029'
ELSE TO_CHAR(DATE, 'MM/DD/YYYY')
END as ENDDATE,
I tried adding in TO_DATE(
before the TO_CHAR
, but I keep getting an error on the Then
part. Any pointers to how to successfully add in TO_DATE
to this statement?我尝试在
TO_CHAR
之前添加TO_DATE(
,但我在Then
部分不断收到错误。有关如何成功将TO_DATE
添加到此语句的任何指针?
You could use date literal (which is always yyyy-mm-dd
) and compare it directly to your date_column
:您可以使用日期文字(始终为
yyyy-mm-dd
)并将其直接与您的date_column
进行比较:
case when trunc(date_column) = date '1800-01-01' then date '2029-01-01'
else date_column
end as enddate
select
case when date_column=to_date('01/01/1800','dd/mm/yyyy') then to_date('01/01/2029','dd/mm/yyyy')
else date_column end as enddate
from dual;
You just have to make sure all the possible outcomes of the case statement belong to the same data type.您只需要确保 case 语句的所有可能结果都属于相同的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.