Database Exercise 4 about ''Date

2018-10-20  本文已影响0人  Drebin

This is Database of Oracle.
And grammar below is suitable to Oralce Database.


to_char(date,format)

presents a date in the specified format.
example:

select to_char(hiredate, 'yyyy-mm-dd') from emp;

result:

image.png

add_months(date,numberic)

adds a number of months to a date (the specified number may be negative)

last_day(date)

shows the last day of its month
example:

select last_day('2018-10-20') from emp;

It will return the result

"2018-10-31"

next_day(date,day)

shows the effect of moving a date forward to a given day of the week.

Sunday -- 1
Monday -- 2
Tuesday -- 3
Wednesday -- 4
Thursday -- 5
Friday -- 6
Saturday -- 7

example:

select next_day('2018-10-20',6) from emp;

results:

'2018-10-26' which is Friday

Exercise 4 Dates

select ename, job, to_char(hiredate, 'MM-DD-YY') from emp2016150071 where deptno = 20
select to_char(hiredate, 'Day-Month-DD-YYYY') from emp2016150071
select * from emp2016150071 where hiredate like '%5月%'
select * from emp2016150071 where to_char(hiredate, 'Day') = '星期二'
select * from emp2016150071 where months_between(sysdate, hiredate) > 16*12
select ename, to_char(round(hiredate), 'Day') from emp2016150071
(select 
  next_day(last_day(hiredate) - 7, '星期五') as payday, ename, hiredate from 
  emp2016150071 
where 
  next_day(last_day(hiredate) - 7, '星期五') > hiredate)
union 
(select 
  next_day(last_day(add_months(hiredate, 1)) - 7, '星期五') as payday, ename, hiredate from 
  emp2016150071 
where 
  next_day(last_day(hiredate) - 7, '星期五') <= hiredate)

The employee Martin is hired after the last Friday of the month
So, we have to handle such problem, which is why there is another SELECT behind 'union'

上一篇 下一篇

猜你喜欢

热点阅读