Solved

Convert fixed months, weeks or years to days in oracle

Posted on 2016-09-16
9
81 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 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
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: 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 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 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 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 configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

860 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