22

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?

William Robertson
  • 15,273
  • 4
  • 38
  • 44
Raj More
  • 47,048
  • 33
  • 131
  • 198

5 Answers5

28

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
  • 1
    for some reason people don't like extract. but, I find it very simple and elegant. – clq Oct 15 '15 at 14:09
22
 SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL
Michael Pakhantsov
  • 24,855
  • 6
  • 60
  • 59
3

I think this is what you are looking for

select to_char(current_timestamp,'HH24') from dual;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
1

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;    
psaraj12
  • 4,772
  • 2
  • 21
  • 30
-1

if you want to get the month name of a date

select to_char( 'month', sysdate())
HK boy
  • 1,398
  • 11
  • 17
  • 25
Alvaro
  • 1