Solved

Convert fixed months, weeks or years to days in oracle

Posted on 2016-09-16
9
154 Views
Last Modified: 2016-09-19
SELECT nvl(mfg_shelf_life, -1)
            ,nvl(mfg_shelf_life_timeframe,'x')
      FROM dss.part_specs pspec
      where mfg_shelf_life_timeframe in ('Y','W','M')

Open in new window


shelf life
M - Months
Y   Years
W  Weeks.
 
Right now I do not have data with weeks.

I need to take that value and convert to days.  Can I get a query to get these values?
0
Comment
Question by:anumoses
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 41801965
The result is simple, it's just multiplication.

The tricky part is what to multiply by..

How long is a month?  28, 29, 30, 31 days? something else, if so what?
How long is a year?  365 or 366 days?  something else, if so what?
0
 
LVL 32

Expert Comment

by:awking00
ID: 41801995
Weeks are easy, they all have 7 days. But years and months are not fixed, you'll need to have dates in order to calculate days.
The days between 01-JAN-2016 and 01-JAN-2017 are 366 but the days between 01-JAN-2017 and 01-JAN-2018 are 365.
The days between 01-JAN-2016 and 01-MAR-2016 are 60 but the days between 01-JUL-2016 and 01-SEP-2016 are 62.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41802012
Rather than multiplying, do you have a start date?  If so, we can use the actual calendar rather than make multiplicative approximations.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:johnsone
ID: 41802025
I posted those calculations in the question you asked yesterday.  You should be able to pull them out of the function, but if you can't, here they are:

weeks to days:

shelf_life_weeks * 7

months to days:

expire_date - Add_months(expire_date, shelf_life_months * -1)

years to days:

expire_date - Add_months(expire_date, shelf_life_years * -12)
0
 
LVL 35

Expert Comment

by:johnsone
ID: 41802029
Not sure why you need the calculations to convert to days, as the function that was posted yesterday took care of that calculation internally.  All you have to do is pass in the number of weeks/months/years and it figures it out.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41802145
Okay. With expire_date and sysdate you have the dates you need to calculate days and johnsone gave you precisely what those calculations are.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 41802592
This question differs slightly (I think) from the question made yesterday.

a. Yesterday the table involved building things
b. Here the table is manufacturer's specifications

for a. there has to be a date of receipt (or similar) otherwise you cannot build/repair
for b. the purpose may be comparisons and not involve any concrete date of receipt

Given that the topic under discussion is aircraft parts then (in my view) you need to be "conservative" (as lives are at risk) so I suggest:

a "year" is 365 days
a "month" is treated as 30 days (round down 365/12)
and a week obviously 7 days

Because the topic is aircraft parts there will probably be standards that apply but I am no longer part of the aerospace industry so I'm hopelessly out of date on those.

so, for approximations in the common unit of days:
SELECT
      part
    , mfg_shelf_life
    , mfg_shelf_life_timeframe
    , mfg_shelf_life 
      * ( case when mfg_shelf_life_timeframe = 'Y' then 365
               when mfg_shelf_life_timeframe = 'M' then 30
               when mfg_shelf_life_timeframe = 'W' then 7
          end ) 
                    AS mfg_shelf_life_in_days_approx
FROM dss.part_specs pspec
WHERE mfg_shelf_life_timeframe IN ('Y', 'W', 'M')

Open in new window

0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 250 total points
ID: 41803351
Regardless of the field names, the math is the same.
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 41804564
Thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question