how to calculate days between two dates in oracle? - oracle

I wanted to find the number of days between two dates in oracle.So,I tried:
select to_date('03/09/2011','MM/DD/YYYY') -to_date('03/09/2010','MM/DD/YYYY') "Days" from dual;
the output is 365.Its ok ,but when I tried:
select to_date(sysdate,'mm/dd/yyyy') -to_date('03/09/2010','MM/DD/YYYY') "Days" from dual;
i got error as:
ORA-01858: a non-numeric character was found where a numeric was expected
And also suppose ,if I calculate the no of days between 2010 and sysdate then the year which contains leap year will be also handled by oracle itself?

you can try this: (sysdate is already date format so you can use like this)
select trunc(sysdate) -to_date('03/09/2010','MM/DD/YYYY') "Days" from dual;

Related

Find records older than 30 days in oracle sql

I am working in Oracle BI with some SQL and trying to find results for a date range from the beginning of last month to 30 days old from today's date. I have looked through all the similar articles on here and tried their coding and none of them worked. This is the latest I have:
FROM "NAME" WHERE "Day Dimension"."Start Date" <= TIMESTAMPADD(SQL_TSI_DAY, -30, DATE'#{day}')
This seemed to get me the closest but Oracle did not recognize the '#{day}' criteria. Can anyone help?
For OBIEE reports:
timestampadd(SQL_TSI_DAY,-30,CURRENT_DATE)
(OR)
SYSDATE, cast(EVALUATE('SYSDATE-30') as timestamp)
For SQLPLUS, Try this- SYSDATE will return the current date; subtract 30 for the number of days:
SELECT *
FROM TABLE
WHERE DATE_FIELD < SYSDATE-30;
References:
http://oracle.ittoolbox.com/groups/technical-functional/oracle-bi-l/how-to-use-sysdate-function-in-obiee-reports-4291644

Date_part does not works in oracle

SELECT
DATE_PART('days',DATE_TRUNC('month', pr.DateTo) '1 MONTH'::INTERVAL - DATE_TRUNC('month', pr.datefrom) as PeriodDays
FROM HT_PayReg
This works well in postgres,but in oracle it does not works,Please correct it in oracle.
TRUNC gives the date excluding the time portion.
TO_CHAR with format mask gives you different parts of a date.
For example,
to_char(sysdate, 'DD') gives today's date as 09.
to_char(sysdate, 'MON') gives current month as OCT.
to_char(sysdate, 'YYYY') gives current year as 2014.
Play around with the different formats. For a specific output, mention your desired output.
Look at datetime format models in documentation here http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm

Months between sysdate to earlier date

I want to get the months between current date to earlier date.
SELECT MONTHS_BETWEEN(to_date(fld_valid_from,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(sysdate, 'yyyy-mm-dd hh24:mi:ss')) num_months
FROM tbl_customer
But it's not working. I don't know if that's correct or not.
sysdate is already a date, and does not need to be converted to one using to_date(). I suspect that fld_valid_from is also a date.
What error message are you getting? Did you try comparing the two variables without the TO_DATE command?
SELECT MONTHS_BETWEEN (trunc(fld_valid_from),trunc(sysdate))
FROM tbl_customer
This would work if your column fld_valid_from is a date type. You're comparing a date with another date. You use TO_DATE to convert a string data type to a date data type.
With the TRUNC function, you remove the timestamp from the date, and get only...well, the date:
SELECT SYSDATE, TRUNC(SYSDATE) FROM DUAL;
SYSDATE TRUNC(SYSDATE)
---------------------------------------
16/07/2013 10:45:53 16/07/2013
Hope it helps.
Regards.

Date as number in sql

When we type current date in Excel cell as 08-May-2013
Right click on the cell and in the format when i click number as category i get a number
Date-08-May-13
Formatted one-41402.00
So is there anyway i can get the same number in sql
I tried using this.
select to_char(sysdate,'J') from dual
But the output is 2456421
I understand that this is a Julian value
But can anyone help me in getting the output as that i am getting on excel i.e; 41402
The Windows version of Excel stores dates as serial numbers. 01-Jan-1900 is 1, 02-Jan-1900 is 2, etc. The Mac version used to use a different starting date; I don't know whether that's still the case.
The essential data you need is in simple date arithmetic.
select current_date, current_date - date '1900-01-01'
from dual;
That returns 41400.67037037037 for my current connection. Rounding up and adding 1 for fenceposting would return the number you're looking for, but I'd want to test that with multiple time zones and such before I'd swear by it.
A date in Excel is stored as a serial number, with 01-JAN-1900 as 1. Citation.
We can do arithmetic with dates in Oracle, so converting to an Excel date from Oracle would be:
trunc(sysdate) - to_date( '1900-01-01', 'yyyy-mm-dd')
I've tested this and infuriatingly it produces 41401 - because it's going from midnight. So obviously Microsoft are using a ceiling function to raise it to the next integer:
ceil (sysdate - to_date( '1900-01-01', 'yyyy-mm-dd') )

Oracle Interval Bug?

I'm using this sql query:
select sysdate, sysdate - INTERVAL '6' month from dual;
But it is return: ORA-01839: date not valid for month specified.
Which is weird, because if I change the the number into 9, it is return the date (sysdate = 31/05/11 and the subtracted is 31/08/10). I'm also tried using different value: 1,3,6,8,11 also not working, but 2,4,5,7,9,12 are working.
From the numbers, I think it is because the resulting quert doesn't have 31 days for that month. Is this the expected behavior? Because in MySQL, I can use the query (select now() - Interval 6 Month;) to get the correct value. Is there any other way?
I am using Oracle 11.1.0.6
It is the expected behaviour; see the sixth bullet in the datetime/interval arithmetic section of the documentation.
As Lisa says you can use add_months, which has the opposite behaviour - which can also cause confusion sometimes. You need to decide which is most suitable for you.
select sysdate,add_months(sysdate,-6) from dual;

Resources