Solved

SQL - SUBTRACT DATES GET YEARS?

Posted on 2014-04-18
4
681 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 77

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 77

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 77

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-00923: FROM keyword not found where expected 3 80
constraint check 2 48
Character matching different date formats for dates between 6 59
Fill Date time Field 12 25
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

829 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