Equivalent function for DATEADD() in Oracle - sql

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

Related

Sysdate in where clause not working in oracle sql

I have below select query where i am trying to get the data only for today date but its not returning anything:
select * from V_TER
where SYSTEM_INSERTED_AT = SYSDATE;
The SYSTEM_INSERTED_DATE is of Date datatype and the value is stored in this fields as for example 2021-01-15 15:17:13
The problem in Oracle is that dates can have time components both in the data and sysdate itself.
I would recommend checking for any time on the current date:
where system_inserted_at >= trunc(sysdate) and
system_inserted_at < trunc(sysdate) + interval '1' day
This is generally optimizer-friendly. If you don't care about that, then:
where trunc(system_inserted_at) = trunc(sysdate)

How to find records from yesterdays time till todays time in sql?

I am trying to find records from yesterdays 10:30 PM till today's 10:30 PM with SQL query. Please help me with sql query to find such records.
Maybe its a duplicate question, if so please link me to that. Don't want any pl-sql function.
A simple way to do this is to subtract times and compare dates. So, one way is:
select t.*
from t
where trunc(datecol) = trunc(sysdate - 1.5/24);
It is more efficient to use a direct comparison (because Oracle can more readily use an index):
select t.*
from t
where datecol >= trunc(sysdate) - 1.5/24 and
datecol < trunc(sysdate) + 1 - 1.5/24;
Note: You can also use interval for this purpose, if you are less old-fashioned than I am:
select t.*
from t
where datecol >= trunc(sysdate) - interval '90' minute
datecol < trunc(sysdate) + interval '1' day - interval '90' minute;
You can get the yesterday date with SYSDATE - 1. You would need something like this:
SELECT ...
FROM ...
WHERE date_field BETWEEN SYSDATE-1 AND SYSDATE

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
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())

Add Day to Timestamp

How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.
select '01-jan-2011 11-09-05' + interval '2' day
A completely Oracle-centric solution is to simply add 2 to the timestamp value as the default interval is days for Oracle dates/timestamps:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + 2
FROM dual;
In a similar case, I used:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + NUMTODSINTERVAL(2, 'DAY')
Because, othewise, the expression is converted to DATE and precission is lost. See: NUMTODSINTERVAL documentation

Common way to compare timestamp in Oracle, PostgreSQL and SQL Server

I am writing an SQL query which involves finding if timestamp falls in particular range of days.
I have written that in the PostgreSQL but it doesn't works in Oracle and SQL Server:
AND creation_date < (CURRENT_TIMESTAMP - interval '5 days')
AND creation_date >= (CURRENT_TIMESTAMP - interval '15 days')
Is there are common way to compare the timestamp across different databases?
I'm not a SQL Server expert but I know this works on Oracle and Postgres and I suspect it may work on MSSQL but have no way to test it ATM.
AND creation_date < (CURRENT_TIMESTAMP - interval '5' day)
AND creation_date >= (CURRENT_TIMESTAMP - interval '15' day)
Or if you are using the date type instead of timestamp, you could do this but I'm pretty sure it wouldn't work on MSSQL. And the DATE type is quite different between Oracle and Pg.
AND creation_date < CURRENT_DATE - 5
AND creation_date >= CURRENT_DATE - 15
As was noted in the comments for OMG Ponies, you can only add ints to Date types not timestamps. (Oracle silently casts the timestamp to date)
How to compare two timestamps in postgresql, the following returns true:
select to_timestamp('2010-01-01 10:10:85.123', 'YYYY-MM-dd HH:MI:SS.MS') <
to_timestamp('2012-01-01 10:10:85.123', 'YYYY-MM-dd HH:MI:SS.MS');
Returns true because the 2012 is after the 2010
I don't believe there is common syntax that'll work across all database engines. In SQL Server, you do it like this:
AND creation_date BETWEEN DateAdd(dd, -5, GetUtcDate()) AND DateAdd(dd, -15, GetUtcDate())
I'm not sure about Oracle...
Oracle (I have tested both of these solutions):
AND (creation_date BETWEEN sysdate-15 AND sysdate-6)
This syntax is also valid:
AND (creation_date BETWEEN current_timestamp - INTERVAL '15' DAY
AND current_timestamp - INTERVAL '6' DAY)
Note that SYSDATE and CURRENT_TIMESTAMP are synonymous (sort of - see comments).
Note that BETWEEN returns values inclusive of the bounds. Since you are looking for values which are >= date-15 but < date-5, you need to specify -15 to -6 when using BETWEEN. The answers using an upper bound of -5 with a BETWEEN expression are wrong.
select * from timing where (db_time < CURRENT_DATE) AND (db_time > (CURRENT_DATE - INTERVAL '1' DAY)) ;
Again this is specific for Oracle, assume intime is TIMESTAMP(3) data type, you can do this
select * from MYTABLE where intime >= to_timestamp('2014-10-01 7:00:56', 'YYYY-MM-dd HH24:MI:SS')