Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, mark.

Asked: May 29, 2020 - 1:03 pm UTC

Last updated: May 29, 2020 - 1:27 pm UTC

Version: 12.1

Viewed 1000+ times

You Asked

I have some code (see below) which tells me the username and last_login for any login not used in the last 15 days.
I would like to add a 3rd column to the results, which shows the number of days sine the last login, to make it easier for the person running the report to see how long the account has been inactive.
Results would look something like USERNAME, LAST LOGIN, NUMBER OF DAYS.

I cannot seem to get it to work. Any suggestions>?

CODE without additional 'NUMBER OF DAYS' coding:


select username, last_login
from
dba_users
where username in 
(select username from all_users where oracle_maintained = 'N')
and 
username NOT IN ('DBAMETRICS')
and last_login <= TRUNC(SYSDATE) - 15
order by last_login ASC;


and Chris said...

Have you tried subtracting the login date from sysdate?

select username, last_login,
       TRUNC(SYSDATE) - trunc ( last_login ) days
from   dba_users
where  username in  (select username from all_users where oracle_maintained = 'N')
and    username NOT IN ('DBAMETRICS')
and    last_login <= TRUNC(SYSDATE) - 15
order by last_login ASC;

USERNAME    LAST_LOGIN                  DAYS   
HR          12-MAY-2020 15.29.45 +01         17 

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Excellent!

mark dickinson, May 29, 2020 - 1:32 pm UTC

Answer works perfectly. Thanks a million!

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library