Oracle week of the year - oracle

Why 12/16/2013 and 12/17/2013 are in different week?
alter session set NLS_TERRITORY=AMERICA;
select to_char(to_date('12-16-2013', 'mm-dd-yyyy'),'ww'),to_char(to_date('12-17-2013', 'mm-dd-yyyy'),'ww') from dual

If you look at the formatting models documentation, it states:
WW - Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.
W - Week of month (1-5) where week 1 starts on the first day of
the month and ends on the seventh.
01/01/2013 started on a Tuesday last year, not the first day of week. So in your test case, 12/17/2013 was on a Tuesday also, and a new "week" as oracle calculates it. Certainly, non-obvious.

Related

Convert Date strored as number(32,0) in oracle

I have an issue importing date from a Tririga database into a SQL database. Mainly I cant convert the date properly and it looks like is not the commont format I have seen around.
Eg date value incomming 775724400000
Running something like select to_date('765187200000', 'DDMMYYYYHH24MISS') my_date FROM dual;
give me an error
ORA-01847: day of month must be between 1 and last day of month
01847. 00000 - "day of month must be between 1 and last day of month"
I found the following info from this link seems to be also from tririga
link_help
And the size of the number are about 10 digits meanwhile this one is 12 and I know for fact this dates should be from the past 10 years (most of them)
I can't seem to find anything that gives me an answer how to convert this into proper dates.
Any suggestions?
The input number appears to be "epoch", a count of milliseconds elapsed since 1 January 1970 GMT (UTC).
To convert to date is not difficult:
select date '1970-01-01' + (775724400000 / 86400000) as dt from dual;
DT
--------------------
1994-Aug-01 07:00:00
Note the hard-coded literals: date '1970-01-01' (epoch is by definition measured from midnight on this date) and 86400000. By one of the definitions (in the previous version of the International System of Units and Weights), a second is 1/86400 of a median day. In Oracle, date arithmetic is based on the number 1 representing one day, so to convert your milliseconds to days you must divide your input by 86400 * 1000.
The most delicate question has to do with time zones (and possibly daylight saving time, also related to time zone). In most cases, epoch is measured from midnight on 1 January 1970 GMT, not from midnight on 1 January 1970 in local time. Do you need to adjust for that? Only you and your business users can answer that question.
(As an aside, the number you provided does NOT represent a date in the past 10 years - not even close.)

convert date to order day of the month

How can I convert date to the order day of the month in ORACLE?
Ex: 31/07/2000 -> "Monday, the Thirty-First of July, 2000".
Is there any format date which can solve this problem?
Thanks so much!
Yes, there is - you need to combine some format elements (and modifiers) with a bit of boilerplate text (to add "the" and "of"). Like this:
select to_char( to_date('31/07/2000', 'dd/mm/yyyy')
, 'fmDay, "the " Ddspth "of" Month, yyyy') as spelled_out_date
from dual;
SPELLED_OUT_DATE
---------------------------------------
Monday, the Thirty-First of July, 2000
Note that, while the names of days of the week and calendar months depend on your session's then-current NLS_DATE_LANGUAGE, the Ddspth element will always be in English. So, alas, this solution DOES NOT WORK for other languages.

Last Sunday of March and October

I want to populate a table ---one column having the date for last Sunday of March and October for each year from 1800 to 2050 ..
Can someone help?
Thanks in advance
SELECT NEXT_DAY(LAST_DAY(SYSDATE)-7, 'SUN') FROM DUAL;
This gives the last date of the current month.
You just need to loop through the months instead of just giving SYSDATE.

ORACLE number of current week

im trying to get the number of current week by this select
to_char(sysdate) is 04.JUN.2014
select to_char(sysdate, 'WW') from dual;
it returns 23, thats okay
but when i do with sysdate-1
select to_char(sysdate-1, 'WW') from dual;
it returns 22, despite yesterday was also 23-rd week. and to_char(sysdate-1) is 03.JUN.2014
i am querying against oracle 11g2
Please, help me understand why this works so?
WW: Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
Jan 1,2014 starts on Wednesday and that's why weeks are starting from Wednesday to Tuesday for this year.
If you use "IW" then weeks will be counting from Monday to Sunday.

months_between for a leap year

When I find months between 28-FEB-11 and 29-FEB-12, months_between function in oracle returns 12. Actually it should be 12.096. This function is not calculating for the leap year proper.
For between 28-FEB-11 and 29-FEB-12, it is 1 year(12 months) and 1 day.
select months_between('28-FEB-12', '28-FEB-11') from dual; -- 12
**select months_between('29-FEB-12', '28-FEB-11') from dual; -- 12**
select months_between('28-FEB-12', '27-FEB-11') from dual; -- 12.0322
select months_between('27-FEB-12', '28-FEB-11') from dual; -- 11.9677
is this an Oracle bug??..
-Vishwa
From the Oracle documentation:
MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
So it's following the documented behavior. It's just not what you expected.
It's not a bug because ORACLE says so, it's a logical error driven by a human (and documenting as something rigth) which is worse.
How come if difference between the last days of January and February months is exactly 1 month (29 exact days). Please see below:
MONTHS_BETWEEN('29-FEB-12','31-JAN-12')
1
With an extra day (30 days) the difference in months is less than 1. Please see below:
MONTHS_BETWEEN('29-FEB-12','30-JAN-12')
.967741935
WRONG. That's not rigth at all!
SQL Server in another hand handle this correctly:
select DATEDIFF(MM,'29-FEB-12','30-JAN-12')
select DATEDIFF(MM,'29-FEB-12','31-JAN-12')
both are 1

Resources