Oracle equivalent to SQL Server DATEPART

三世轮回 提交于 2019-12-04 22:40:39
 SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL

An alternative is the EXTRACT function which is an ANSI standard and also works on other DBMS, but it also requires the use of current_timestamp (also ANSI) instead of sysdate

SELECT extract(hour from current_timestamp) 
FROM dual
Ranjith M R

I think this is what you are looking for

select to_char(current_timestamp,'HH24') from dual;

for additional ways of using DATEPART kind of function in oracle

DATEPART:-Weekday

--This would give the day for the week for a reference start day

DECLARE
Start_day date:=to_date('30-03-2012','DD-MM-YYYY');
check_date DATE:=SYSDATE;
Day_of_week NUMBER;
i NUMBER;
BEGIN
i:=to_char(Start_day,'D');
day_of_week:=mod((to_char(check_date,'D') -(i-1)+7),7);
if day_of_week=0
THEN
day_of_week:=7;
end if;
dbms_output.put_line(day_of_week);
END;    
Alvaro

if you want to get the month name of a date

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