oracle 11g date function related - - sql

How do we come to know whether the given 2 digit year code(YY) is of 19XX or 20XX? For example if we type 07-apr-10, then is 10 the year 1910 or 2010?

Generally speaking, you should be using the RR datetime format element to convert two-digit years to full years.
If the specified two-digit year is 00 to 49, then
If the last two digits of the current year are 00 to 49, then the returned year has the same first two digits as the current year.
If the last two digits of the current year are 50 to 99, then the first 2 digits of the returned year are 1 greater than the first 2 digits of the current year.
If the specified two-digit year is 50 to 99, then
If the last two digits of the current year are 00 to 49, then the first 2 digits of the returned year are 1 less than the first 2 digits of the current year.
If the last two digits of the current year are 50 to 99, then the returned year has the same first two digits as the current year.
I don't think you want YY, as it always fills the missing digits with ones from the current year. For example:
SELECT TO_CHAR(TO_DATE('98', 'YY'), 'YYYY') FROM dual; -- returns 2098, not 1998
SELECT TO_CHAR(TO_DATE('9', 'Y'), 'YYYY') FROM dual; -- returns 2019
SELECT TO_CHAR(TO_DATE('98', 'RR'), 'YYYY') FROM dual; -- returns 1998 (until we get to 2050)

EDIT: Sorry my client setting only had YY in it. After adding YYYY, I am getting below results. So looks like by default YY takes 20YY
select to_date('7/10/15','DD/MM/YY') from dual;
07-OCT-2015
select to_date('7/10/15','DD/MM/YYYY') from dual;
07-OCT-0015

To check this :
First of all you need to convert a CHAR with YY year format into a date:
TO_DATE('20-01-15','DD-MM-YY');
and after that immediately convert it back to CHAR, and see output, like below:
SELECT TO_CHAR(TO_DATE('20-01-15','DD-MM-YY'),'YYYY') FROM DUAL;

Related

Convert decimal year to date

I have dates in a table that are stored as decimal years. An example is 2003.024658 which translates to January 9, 2003.
I would like to convert the decimal years to Oracle's date format.
I've found someone who's done this in Excel: Decimal year to date formula?
=DATE(INT(B1),1,MOD(B1,1)*(DATE(INT(B1)+1,1,1)-DATE(INT(B1),1,1)))
However, I can't quite figure out how to convert the logic to Oracle PL/SQL.
If you start from the assumption that the decimal portion was calculated according to the number of days in the given year (i.e. 365 or 366 depending on whether it was a leap year), you could do something like this:
with
q1 as (select 2003.024658 d from dual)
,q2 as (select d
,mod(d,1) as decimal_portion
,to_date(to_char(d,'0000')||'0101','YYYYMMDD')
as jan01
from q1)
,q3 as (select q2.*
,add_months(jan01,12)-jan01 as days_in_year
from q2)
select d
,decimal_portion * days_in_year as days
,jan01 + (decimal_portion * days_in_year) as result
from q3;
d: 2003.024658
days: 9.00017
result: 10-JAN-2003 12:00am

How to get how many days passed since start of this year?

I have a query which uses needs to know how many days passed since 1st of January in the current year.
Which means that if the query runs for example in:
2nd Jan 2017 than it should return 2 (as 2 days passed since 1st Jan
2017).
10th Feb 2016 than it should return 41 (as 41 days passed since 1st
Jan 2016).
basically it needs to take Current Year from Curent Date and count the days since 1/1/(Year).
i have the current year with: SELECT EXTRACT(year FROM CURRENT_DATE);
I created the 1st of Jan with:
select (SELECT EXTRACT(year FROM CURRENT_DATE)::text || '-01-01')::date
How do I get the difference from this date to Current_Date?
Basically this question can be Given two dates, how many days between them?
Something like age(timestamp '2016-01-01', timestamp '2016-06-15') isn't good because I need the result only in days. while age gives in years,months and days.
An easier approach may be to extract the day of year ("doy") field from the date:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE);
date_part
-----------
41
And if you need it as a number, you could just cast it:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE)::int;
date_part
-----------
41
Note: The result 41 was produced by running the query today, February 9th.
Given two dates, how many days between them
Just subtract one from the other.
In your case you could just round the current_date to the start of the year and subtract that from the current date:
select current_date - date_trunc('year', current_date)::date
The ::date cast is necessary to get the result as an integer, otherwise the result will be an interval.
Another solution is to use DATEDIFF
SELECT DATE_PART('day', now()::timestamp - '2016-01-01 00:00:00'::timestamp);

PL/SQL Food Industry "Julian" Date Conversion

I have some 5 digits dates in a format that the food industry generally calls "Julian" dates (Warning: these are not the "Julian" dates you are familiar with. It's misused terminology that "stuck" and became the standard in the industry. Don't comment on that, it's just how it is.)
The first 3 digits of these "Julian" dates are a number representing the day of a year.
Example:
January 1 = 001
January 2 = 002
December 31 = 365 or 366
The next two digits are the last two digits of the year. 2015 = 15.
For example 22215 = August 10, 2015 (I believe).
I need an Oracle SQL statement that convers these "dates" into standard dates to join to other date data.
I'd add (count of days) -1 to 1st Jan of year
select to_date('20'||15||'01.01','yyyy.mm.dd') + 222-1 from dual
10/08/2015
A co-worker found the answer. The "DDD" format:
SELECT to_date('36515', 'DDDYY') FROM DUAL

Year 2014 getting converted to 1914 in BODS job

The current arrangement is:
SAP DSO: contains the data columns in the form of dd-mm-yyyy.
The BODS job fetches the data from the DSO and loads to the landing table.
The corresponding date column in Teradata is dd-mm-yy.
When the dates are loaded to teradata, year 2014 is converted to 1914.
There is no transformation involved. Direct mapping between the source and target.
This issue started happening only a few months back. Not sure what to check.
To show the correct year in your date, use the DateTime format dd-mm-rrrr.
When dates are stored in one century but refer to another century, dates can be shown with the wrong prefix. The rrrr year format works as so;
If the specified two-digit year is 00 to 49, then
If the last two digits of the current year are 00 to 49, then the
returned year has the same first two digits as the current year.
If the last two digits of the current year are 50 to 99, then the
first 2 digits of the returned year are 1 greater than the first 2
digits of the current year.
If the specified two-digit year is 50 to 99, then
If the last two digits of the current year are 00 to 49, then the
first 2 digits of the returned year are 1 less than the first 2 digits
of the current year.
If the last two digits of the current year are 50 to 99, then the
returned year has the same first two digits as the current year.
I came across a similar issue with BOXI 3.1 and Oracle.
After creating a number of tables with Date fields with the DateTime format as dd/mm/yyyy, and building a universe with this format in mind, I noticed on testing that some of the date results were displaying incorrectly e.g. 01/07/1993 was displaying as 01/07/2093. This was due to the data being loaded into the table having only 2 yy digits e.g. 01/07/93where as Oracle was expecting a DateTime format with 4 yyyy digits.
In turn, Oracle was forcing the year format into 4 digits but as the year was in the last century (20th) but stored in the 21st Century, the wrong century was prefixed onto the year.
To solve, I used the rrrr DateTime Format for the year. Full explaination from Oracle at this link, and further explaination can be found here.
When I recreated the table with the DateTime format as dd-mm-rrrr, the date displayed correctly.
I hope this helps.
DBSControl for CenturyBreak specifies which two-digit years are interpreted as 20th century and which are interpreted as 21st century. If your system has this configured as a non-zero value, which is the default, than this is quite possibly the reason you are seeing the behavior with your data.
CenturyBreak does not affect four digit years or dates input as numeric.
If CenturyBreak=10, strings such as '00/01/01' and '09/01/01' are interpreted as 2000 and 2009. A string inserted as '14/01/01' is interpreted as 1914.
Check with your DBA to determine if the CenturyBreak has been set to a non-zero value or explicitly convert your data inputs to numeric date values.

What is accuracy in "date" column type in sql server 2008

I found below statement regarding "date" column type:
"date" stores only the date component
without the time component, ranging
from 1st January 0001 to 31st December
9999, with accuracy of 1 day
I am not able to understand what is meaning of accuracy here?
I found this statement on:
http://www.sqlservercentral.com/articles/News/3253/
day because it's "date"
Read The ultimate guide to the datetime datatypes by Tibor Karaszi
It means that the minimum difference from date to date is a single day, nothing less.
In other words, you can't store hours in a date column.
Or, that the data is accurate to within a day.
See date on MSDN:
Range - 0001-01-01 through 9999-12-31. January 1, 1 A.D. through December 31, 9999 A.D.
Accuracy - One day
The date type stores the value internally as an integer, meaning the number of days since 0001-01-01. The value 0001-01-03 for example would be stored internally as 2.
The term "accuracy" is probably used here because it's used to express the resolution of other date/time types. The resolution for the date type is simply one day, just as you would expect. The accuracy (resolution) for the datetime type for example is 3.33 milliseconds.
I agree the use of "accuracy" is poor wording.
Personally, I would employ the phrase, "its smallest time granule is one day."
Accuracy?
It does what it says. It stores a date, not time...
From date (Transact-SQL)
Element ranges
YYYY is four digits from 0001 to 9999
that represent a year.
MM is two digits from 01 to 12 that
represent a month in the specified
year.
DD is two digits from 01 to 31,
depending on the month, that represent
a day of the specified month.
where as
from datetime (Transact-SQL)
Element ranges
YYYY is four digits from 1753 through
9999 that represent a year.
MM is two digits, ranging from 01 to
12, that represent a month in the
specified year.
DD is two digits, ranging from 01 to
31 depending on the month, that
represent a day of the specified
month.
hh is two digits, ranging from 00 to
23, that represent the hour.
mm is two digits, ranging from 00 to
59, that represent the minute.
ss is two digits, ranging from 00 to
59, that represent the second.
n* is zero to three digits, ranging
from 0 to 999, that represent the
fractional seconds.