简体   繁体   中英

Oracle equivalent to SQL Server DATEPART

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?

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
 SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL

我想这就是你要找的东西

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;    

如果你想得到一个日期的月份名称

select to_char( 'month', sysdate())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM