Solved

Convert fixed months, weeks or years to days in oracle

Posted on 2016-09-16
9
49 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 73

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 31

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 73

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
 
LVL 34

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

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 31

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 48

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 34

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to recover a database from a user managed backup

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now