Solved

SQL - SUBTRACT DATES GET YEARS?

Posted on 2014-04-18
4
664 Views
Last Modified: 2014-04-18
I'm trying to figure out a query function in a Oracle SQL database.

I'm trying to have the query generate the number of years between two dates.

This is my query so far:

select	 "EMPLOYEE"."EMP_NUM" as "EMP_NUM",
	 "EMPLOYEE"."EMP_LNAME" as "EMP_LNAME",
	 "EMPLOYEE"."EMP_FNAME" as "EMP_FNAME",
	 "EMPLOYEE"."EMP_HIREDATE" as "EMP_HIREDATE",
	   TO_CHAR (SYSDATE, 'DD-MON-YY') as "QUERY_DATE"
 from	 "EMPLOYEE" "EMPLOYEE"

Open in new window


I need a column between EMP_HIREDATE and QUERY_DATE called YEARS_EMPLOYED. Like so:
years
It will state for example "11" in the row if the employee has been hired that long.

I'm really at a lost on how to do this. I imagine it obvious deals with subtracting the two dates, but I'm not quite sure where to start. I imagine that maybe I have to subtract the two dates, then divide by 365? But what commands would be used for that, that will work with the above script anyways that I have so far..?
0
Comment
Question by:Pancake_Effect
  • 3
4 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40009788
Do you want to round up or down?

Check out MONTHS_BETWEEN(sysdate, EMP_HIREDATE)

sysdate is the current OS date.

That gives you MONTHS.  Then all you need to do is divide by 12:

 MONTHS_BETWEEN(sysdate, EMP_HIREDATE) / 12

That gives you fractional results.

Then you can FLOOR, CEIL or ROUND the result.

The online docs tell you what all those functions do:
docs.oracle.com/cd/E11882_01/server.112/e41084/functions067.htm
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40009791
Get in the habit of not using quotes.  It forces case sensitivity in Oracle.  I realize some tools add them for you but if you are writing your own, don't use them.

Also use table aliasing.  You do but you aliased the employee table as employee.

Also for single tables, you don't need it.

See if this works:
select	 E.EMP_NUM,
	 E.EMP_LNAME,
	 E.EMP_FNAME,
	 E.EMP_HIREDATE,
	   TO_CHAR (SYSDATE, 'DD-MON-YY') QUERY_DATE,
	 floor(months_between(sysdate, emp_hiredate)/12) Years_worked
 from	 EMPLOYEE E;

Open in new window

0
 
LVL 4

Author Comment

by:Pancake_Effect
ID: 40009799
Thanks! I was was very close, I was just trying to minus two other fields, instead of simply the SYSDATE and the other time. Didn't know about that Floor or Round function though.

Works perfect, and thank you for that link. That's going to be very helpful.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40009805
Glad to help.

The online docs will become your best friend!

At times I still scan the SQL reference and read about functions that I don't recognize and I've been using Oracle for over 20 years.  Every new release provides new ones and I never knew all the old ones!

As you pointed out, subtracting two dates gives you 'days' but dividing by 365 can lead to inaccurate results.
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

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…
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…
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 copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

746 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

13 Experts available now in Live!

Get 1:1 Help Now