Oracle equivalent to SQL Server DATEPART - sql

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

I think this is what you are looking for
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;

if you want to get the month name of a date
select to_char( 'month', sysdate())

Related

ORA-01846: not a valid day of the week in procedure

Hello In my oracle sql procedure , I am trying to get first sunday of selected year with code below
select next_day(to_date('01.01.'||v_year,'DD.MM.YYYY'), 'Sunday') into v_py from dual;
v_year is number format like 2020 and v_py is date format
but this give me error like title.
When I wrote this query like below then I can see the result. Why isn't it working in procedure
select next_day(to_date('01.01.'||2020,'DD.MM.YYYY'), 'Sunday') from dual;
Thanks in advance
Your code is valid. However, the second argument to next_day() is language-dependent, so I suspect that your database is not English.
You can check the language of the database with the following query:
select * from v$nls_parameters where parameter = 'NLS_LANGUAGE';
From there on, you can change the second parameter from 'Sunday' to the corresponding day name in the database language.
It is also possible to build a language-indenendent expression, taking advantage of the fact that to_char() supports passing a language as last argument (unlike nextday()):
select trunc(to_date(v_year,'YYYY'), 'YYYY')
+ 7
- to_char(trunc(to_date(v_year,'YYYY'), 'YYYY'), 'D', 'NLS_DATE_LANGUAGE=ENGLISH')
into v_py from dual;
trunc(to_date(v_year,'YYYY'), 'YYYY') is just another way to get the first day of the year from the given parameter.
Function NEXT_DAY() depends on NLS_DATE_LANGUAGE, TRUNC(..., 'D') depends on NLS_TERRITORY, so either are not suitable for an NLS independent solution.
Use TRUNC(..., 'IW') which is based on ISO-8601 where begin of the week is always defined on Monday. Would be this:
v_py := TRUNC(TO_DATE('01.01.'||v_year, 'DD.MM.YYYY')+7, 'IW') - 1;
There is no need for SELECT ... INTO ... FROM dual

DATEDIFF function in Oracle [duplicate]

This question already has answers here:
Calculate difference between 2 date / times in Oracle SQL
(21 answers)
Closed last year.
I need to use Oracle but DATEDIFF function doesn't work in Oracle DB.
How to write the following code in Oracle? I saw some examples using INTERVAL or TRUNC.
SELECT DATEDIFF ('2000-01-01','2000-01-02') AS DateDiff;
In Oracle, you can simply subtract two dates and get the difference in days. Also note that unlike SQL Server or MySQL, in Oracle you cannot perform a select statement without a from clause. One way around this is to use the builtin dummy table, dual:
SELECT TO_DATE('2000-01-02', 'YYYY-MM-DD') -
TO_DATE('2000-01-01', 'YYYY-MM-DD') AS DateDiff
FROM dual
Just subtract the two dates:
select date '2000-01-02' - date '2000-01-01' as dateDiff
from dual;
The result will be the difference in days.
More details are in the manual:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#i48042
You can simply subtract two dates. You have to cast it first, using to_date:
select to_date('2000-01-01', 'yyyy-MM-dd')
- to_date('2000-01-02', 'yyyy-MM-dd')
datediff
from dual
;
The result is in days, to the difference of these two dates is -1 (you could swap the two dates if you like). If you like to have it in hours, just multiply the result with 24.
We can directly subtract dates to get difference in Days.
SET SERVEROUTPUT ON ;
DECLARE
V_VAR NUMBER;
BEGIN
V_VAR:=TO_DATE('2000-01-02', 'YYYY-MM-DD') - TO_DATE('2000-01-01', 'YYYY-MM-DD') ;
DBMS_OUTPUT.PUT_LINE(V_VAR);
END;

Equivalent function for DATEADD() in Oracle

I have to get a date that is 6 months from the system date in Oracle. And I have to get it by running an open-query from SQL. DATEADD(MONTH,-6, GETDATE()) function serves the purpose in SQL.
Does the function DATEADD(MONTH,-6, GETDATE()) in SQL have an equivalent function in Oracle?
Method1: ADD_MONTHS
ADD_MONTHS(SYSDATE, -6)
Method 2: Interval
SYSDATE - interval '6' month
Note:
if you want to do the operations from start of the current month always, TRUNC(SYSDATE,'MONTH') would give that. And it expects a Date datatype as input.
Not my answer :
I wasn't too happy with the answers above and some additional searching yielded this :
SELECT SYSDATE AS current_date,
SYSDATE + 1 AS plus_1_day,
SYSDATE + 1/24 AS plus_1_hours,
SYSDATE + 1/24/60 AS plus_1_minutes,
SYSDATE + 1/24/60/60 AS plus_1_seconds
FROM dual;
which I found very helpful. From http://sqlbisam.blogspot.com/2014/01/add-date-interval-to-date-or-dateadd.html
Equivalent will be
ADD_MONTHS( SYSDATE, -6 )
--ORACLE SQL EXAMPLE
SELECT
SYSDATE
,TO_DATE(SUBSTR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)),1,10),'YYYY-MM-DD')
FROM DUAL

Extract number from the date in pl sql

Does anyone know if it is possible to take the date from a random date in pl sql.
example.
SELECT SYSDATE FROM DUAL
and here the output would be say : 26-10-2010 13:30:34
Now I want to have just the date as a number. In this case that would be 26.
Or is there some sort of function like IsNum that can recognize it for me. So I can just take 26 and leave the rest out.
You can also use EXTRACT(), like so:
SELECT EXTRACT(DAY FROM SYSDATE) AS DAY FROM DUAL;
select to_char(sysdate,'DD') as day from dual
You can use
to_char(SYSDATE, 'DD')
More you can read here: LINK
All format models are described in the official SQL Reference, take a look in case you need something else
select count(*) from
(select sysdate+rownum dates from dual connect by rownum < (sysdate+15)-(sysdate))
where to_char(dates,'D') <> '1' and to_char(dates,'D') <> '7'

How to trunc a date to seconds in Oracle

This page mentions how to trunc a timestamp to minutes/hours/etc. in Oracle.
How would you trunc a timestamp to seconds in the same manner?
Since the precision of DATE is to the second (and no fractions of seconds), there is no need to TRUNC at all.
The data type TIMESTAMP allows for fractions of seconds. If you convert it to a DATE the fractional seconds will be removed - e.g.
select cast(systimestamp as date)
from dual;
I am sorry, but all my predecessors seem to be wrong.
select cast(systimestamp as date) from dual
..does not truncate, but rounds to the next second instead.
I use a function:
CREATE OR REPLACE FUNCTION TRUNC_TS(TS IN TIMESTAMP) RETURN DATE AS
BEGIN
RETURN TS;
END;
For example:
SELECT systimestamp
,trunc_ts(systimestamp) date_trunc
,CAST(systimestamp AS DATE) date_cast
FROM dual;
Returns:
SYSTIMESTAMP DATE_TRUNC DATE_CAST
21.01.10 15:03:34,567350 +01:00 21.01.2010 15:03:34 21.01.2010 15:03:35
On the general topic of truncating Oracle dates, here's the documentation link for the format models that can be used in date trunc() AND round() functions
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions242.htm#sthref2718
"Seconds" is not listed because the granularity of the DATE datatype is seconds.
I used function like this:
FUNCTION trunc_sec(p_ts IN timestamp)
IS
p_res timestamp;
BEGIN
RETURN TO_TIMESTAMP(TO_CHAR(p_ts, 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI');
END trunc_sec;
trunc work to min only, cast to date to_char(START_TIME,'YYYYMMDDHH24MISS')
or simply select to_char(current_timestamp, 'YYYYMMDDHH24MISS') from dual;
https://www.techonthenet.com/oracle/functions/trunc_date.php
To truncate a timestamp to seconds you can cast it to a date:
CAST(timestamp AS DATE)
To then perform the TRUNC's in the article:
TRUNC(CAST(timestamp AS DATE), 'YEAR')
Something on the order of:
select to_char(current_timestamp, 'SS') from dual;