We need to get the HOUR out of a DATETIME column (expecting values from 0 to 23 to be returned).
我们需要从DATETIME列中获取HOUR(期望返回0到23之间的值)。
Is there an Oracle equivalent of the SQL Server DATEPART function?
是否有Oracle等效的SQL Server DATEPART功能?
4 个解决方案
#1
14
SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL
#2
21
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
另一种选择是EXTRACT函数,它是ANSI标准,也适用于其他DBMS,但它也需要使用current_timestamp(也是ANSI)而不是sysdate
SELECT extract(hour from current_timestamp)
FROM dual
#3
1
for additional ways of using DATEPART kind of function in oracle
有关在oracle中使用DATEPART类函数的其他方法
DATEPART:-Weekday
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;
#4
1
I think this is what you are looking for
我想这就是你要找的东西
select to_char(current_timestamp,'HH24') from dual;
#1
14
SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL
#2
21
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
另一种选择是EXTRACT函数,它是ANSI标准,也适用于其他DBMS,但它也需要使用current_timestamp(也是ANSI)而不是sysdate
SELECT extract(hour from current_timestamp)
FROM dual
#3
1
for additional ways of using DATEPART kind of function in oracle
有关在oracle中使用DATEPART类函数的其他方法
DATEPART:-Weekday
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;
#4
1
I think this is what you are looking for
我想这就是你要找的东西
select to_char(current_timestamp,'HH24') from dual;