我无法让我的案例陈述发挥作用,这是我到目前为止所做的事情
Select Employee_id,First_name, Last_name, Job_id, Case Hire_date,
From Employees
Order by to_char(Hire_date, 'MM') ASC, to_char(hire_date, 'DD') ASC;
我也试过
Select Employee_id,First_name, Last_name, Job_id
From Employees
Anniversary = Case
When hire_date = sysdate - 2
Then 'Day before yesterday'
When hire_date = sysdate - 2
Then 'Yesterday'
End 'Anniversary'
Order by to_char(Hire_date, 'MM') ASC, to_char(hire_date, 'DD') ASC;
答案 0 :(得分:1)
直接回答你的问题:
Select
Employee_id,First_name, Last_name, Job_id,
Case
When hire_date = trunc (sysdate) - 2 Then 'Day before yesterday'
When hire_date = trunc (sysdate) - 1 Then 'Yesterday'
when hire_date = trunc (sysdate) + 1 then 'Tomorrow'
// etc etc
End as "Anniversary"
From Employees
Order by to_char(Hire_date, 'MM') ASC, to_char(hire_date, 'DD') ASC;
case
只是一个函数 - 在select或where子句中使用它内联。
使用单引号时,表示值。双引号表示标签(或字段),看起来像你想要的"周年纪念。"
此外,我认为您需要sysdate
,而不是提供日期和时间的trunc (sydate)
。
这是您正在寻找的基本语法。我还要注意你的逻辑,我不认为,是正确的。您需要考虑租用日期与当前sysdate
不同的事实。
这有点笨拙 - 可能有一种更优雅的方式,但这是一个例子,说明如何将雇用日期与当前日期同步。请记住,这种方法可能会在年初和年底适得其反(即1/1只比12/31只有一天,但这样做会让它看起来像是在364天之后) :
select
hire_date,
to_date (extract (year from sysdate) || '-' ||
extract (month from hire_date) || '-' ||
extract (day from hire_date), 'YYYY-MM-DD') as cy_hire_date
from Employees
希望这足以让你到达你需要去的地方。
答案 1 :(得分:0)
你有when hire_date = sysdate - 2
两次。这将是一个开始的地方。您的case语句中的语法也不正确。它应该在FROM子句之前。
看起来Hambone给了你答案。无论如何,它的整个CASE部分应该被理解为类似于您选择的字段。 AS可以与任何字段一起使用:select employee_id as ID
等。