在 Oracle 中使用 SYSDATE 编写 IF 语句时需要帮助
我有一张员工表。我必须以这种方式获取所有员工的数据。如果今天我运行查询,那么我必须获取上一年12月1日(当前年份-1,即2010年12月1日)到今天(4月21日)之间工作的所有员工的数据。如果查询运行日期是 12 月(例如今天是 12 月 15 日),则查询应获取从当年 12 月 1 日(2011 年 12 月 1 日)到 12 月 15 日的数据。
谁能帮我写下这种情况下的 IF 或 CASE 语句。我写了 if 语句一些它不起作用的原因。我想利用这个 If 或 Case 语句作为 employee_timestamp 的开始日期。这在这里可能吗?
SELECT e.*
FROM EMPLOYEE e
WHERE e.employee_timestamp > (SELECT TO_DATE(TO_CHAR(CONCAT('12-01-', EXTRACT(YEAR FROM SYSDATE)-1)),'MM/DD/YYYY') AS startdate
FROM DUAL)
AND e.employee_timestamp < (SELECT SYSDATE FROM DUAL)
IF
((SELECT SYSDATE FROM DUAL)) > (select to_date(to_char(concat('11-30-', extract(YEAR FROM sysdate))),'MM/DD/YYYY') as Startdate From DUAL)
THEN (select to_date(to_char(concat('12-01-', extract(YEAR FROM sysdate))),'MM/DD/YYYY') as Startdate From DUAL)
ELSE
(select to_date(to_char(concat('12-01-', extract(YEAR FROM sysdate)-1)),'MM/DD/YYYY') as Startdate From DUAL)
END IF;
I have an employee table. I Have to get the data of all employees in such a way that. If today I run the Query,then I have to get the data of all employees working between december 1st of previous year(current year-1 i.e., december 1st 2010.) till today(april 21st). If the query run date is in the month of december(example today is december 15th) then the query should get the data from december 1st of current year(december 1st 2011) to December 15th.
Can anyone help me writing down the IF or CASE Statement in this case. I wrote the if statement some how its not working. I want to make use of this If or Case Statement as the start date of the employee_timestamp. Is this possible here or not.
SELECT e.*
FROM EMPLOYEE e
WHERE e.employee_timestamp > (SELECT TO_DATE(TO_CHAR(CONCAT('12-01-', EXTRACT(YEAR FROM SYSDATE)-1)),'MM/DD/YYYY') AS startdate
FROM DUAL)
AND e.employee_timestamp < (SELECT SYSDATE FROM DUAL)
IF
((SELECT SYSDATE FROM DUAL)) > (select to_date(to_char(concat('11-30-', extract(YEAR FROM sysdate))),'MM/DD/YYYY') as Startdate From DUAL)
THEN (select to_date(to_char(concat('12-01-', extract(YEAR FROM sysdate))),'MM/DD/YYYY') as Startdate From DUAL)
ELSE
(select to_date(to_char(concat('12-01-', extract(YEAR FROM sysdate)-1)),'MM/DD/YYYY') as Startdate From DUAL)
END IF;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先是一些日期操作示例,无需将所有内容转换为字符串或从字符串转换回来。
如果当前月份不是 2011 年 12 月,最后一次解码将返回 2010 年 12 月 1 日。如果您在 2011 年 12 月运行它,它将返回 2011 年 12 月 1 日。
使用以下语句:
现在,根据您想要的日期范围,您可以 日期范围是包含的
如果日期范围与您指定的完全相同(排除)
希望这能回答您的问题:)
请记住记录下来,这样的代码多年后很难分析。
First some date manipulation examples without converting everything to and back from strings.
The last decode returns December 1st 2010 if the current month is NOT December 2011. If You run it on December 2011 it will return the 1st of December 2011.
Now depending on how you want your date ranges you can use the following statements:
If the date ranges are INCLUSIVE
If the date ranges are exactly like you specified (EXCLUSIVE)
Hope this answers your question :)
Remember to document this, code like this is hard to analyse years later.
如果没有 if 语句,这应该让您在查询中使用所需的开始日期:
至少如果我正确理解了要求......
Without an if-statement this should get you the desired start-date to use in your query:
At least if I understood the requirements correctly...
似乎应该是
e.employee_timestamp >= ...
而不是e.employee_timestamp > ...
,但当然,您应该更了解。Seems like it should be rather
e.employee_timestamp >= ...
thane.employee_timestamp > ...
, but you should know better, of course.