Oracle equivalent to SQL Server DATEPART

核能气质少年 提交于 2019-12-06 17:04:07

问题


We need to get the HOUR out of a DATETIME column (expecting values from 0 to 23 to be returned).

Is there an Oracle equivalent of the SQL Server DATEPART function?


回答1:


 SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL



回答2:


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



回答3:


I think this is what you are looking for

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



回答4:


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;    



回答5:


if you want to get the month name of a date

select to_char( 'month', sysdate())


来源:https://stackoverflow.com/questions/5979261/oracle-equivalent-to-sql-server-datepart

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