How to find the issue with dual in oracle? - oracle

I am using Oracle SQL developer. I am using the following query to get the current time stamp.
select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMMSS') from dual;
In this, minutes is constantly set to 10. But when we don't use to_char, it is working fine. How to find what went wrong? Is there any method to correct this?

You should use MI in HHMMSS instead of MM. MI stands for minutes, MM is for months, and currently it is October, hence the 10.
You can find the available formatting options at Oracle's site.

select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMMSS') from dual;
The issue is with the HHMMSS - MM is used to represent the month number. MI is what is used to represent minutes.
So what you're really after is:
select to_char(CURRENT_TIMESTAMP,'DDMMYYYY/HHMISS') from dual;

Related

Where results displayed are less than current time

On Oracle SQL Developer, I'm looking to write a where function that gives me the last 7 days of data. I can write that part myself fine, but the extra part that I need to add on to the end is that I only want results that are for the past 7 days before my current time.
For example, if I query at 14:00 today, I would want it to return results for the past 7 days with data only up until 14:00, as opposed to the full day.
Is this possible?
SYSDATE is the Oracle built in date function that supplies the current date / time accurate to 1 second. All you have to do is:
select *
from whatever
where whatever.datecol between sysdate - 7 and sysdate;

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;

Sybase equivalent in Oracle

I am doing a change of code from Sybase to Oracle.
I have problem in converting the below query to oracle.
Select Custodian_addr,convert(datetime,dateadd(ss,CreateDT,"01/01/1970")
Here CreateDT is the column name whose value for instance is 1015015173
The result for date conversion (for this example)is March 1 2002 8:39 PM GMT
I researched and found an oracle alternative which results in error
Select Custodian_addr,to_char(CreateDT,"SS")
I am getting a query error in Oracle.I am not able to identify whats wrong. Since I am executing this in Perl ["] has to escaped or what might be the issue? Please suggest me a solution
SELECT 'some address' as custodian_addr,
date '1970-01-01' + 1015015173/86400 as create_dt
from dual
/
CUSTODIAN_AD CREATE_DT
------------ -------------------
some address 2002-03-01 20:39:33
Oracle date arithmetic is pretty simple -- adding 1 to a date increments it by 1 day. So since that number is seconds, dividing it by 86400 (60*60*24) casts that number as a number of days (and fractions thereof).
What is the error you get?
Your TO_CHAR function should not use double quotes. It needs to be
TO_CHAR(CreateDT,'SS')
If you put something in double quotes in Oracle, it's interpreted as an identifier, not a string constant.

In Oracle, why does this return March 1st?

In Oracle, this returns 03/01/2010. That does not make sense to me. Anybody know why?
SELECT TO_DATE( '2010' ,'yyyy' ) AS STRANGE_YEAR_RESULT
FROM DUAL
I've tried on Oracle 10g and 11g.
Oracle needs a complete DateTime in its Date type value field, thus making it take the first day of the current month, I would guess, since you required no other information than the year. Remember that you always need to cast through TO_DATE() and TO_CHAR() dates in Oracle. Assuming so, Oracle "knows" that you will get the information required.
I don't think there is any sensible reason, it's just "what it does". It also got discussed on the OTN forums about a year ago.
Don't know, but my guess is that months are zero based, so Jan = 0, Mar = 2, etc.
"10" might be a Y2K problem in the making, but it's being interpreted as 2010.
And if no day of month is given, perhaps it's assuming the first day of the month.
Why test this? You'd never want to code this way.

Resources