Oracle 相当于 SQL Server DATEPART

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5979261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:30:21  来源:igfitidea点击:

Oracle equivalent to SQL Server DATEPART

sqloracledatedatepart

提问by Raj More

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?

是否有与 SQL Server DATEPART 函数等效的 Oracle?

回答by a_horse_with_no_name

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

回答by Michael Pakhantsov

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

回答by Ranjith M R

I think this is what you are looking for

我想这就是你要找的

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

回答by psaraj12

for additional ways of using DATEPART kind of function in oracle

有关在 oracle 中使用 DATEPART 类型函数的其他方法

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;    

回答by Alvaro

if you want to get the month name of a date

如果您想获取日期的月份名称

select to_char( 'month', sysdate())