SQL Table

phman1275
phman1275 used Ask the Experts™
on
Hi, I need to create a SQL Table "Employees" table with the following fields:

ID, Name, Hiredate, Seniority

123, Johm, 11/8/1984, ????

For the Seniority field: if the current date - hiredate < 1 then 'Newbie'

****************************************************
My question is, should I put the seniority field as a calculation field? Or should I use the trigger to determine the employee is 'Newbie', 'Junior', or 'Senior'?

I have no clue how to either use the calculation to calculate the Seniority or use the trigger to do that.. Please help...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
I have no clue how to either use the calculation to calculate the Seniority or use the trigger to do that.. Please help...

it depends on your requirement. To make this easy, you can use a calculated field for Seniority in a View or in Stored Procedure. And you no need to physically store it as a different field.
As Ryan says, It depends. Do you want this value calculated at the time of row creation or row retrieval....

Assuming you want it at time of row CREATION then try a trigger

If you want it calculated at time of RETRIEVAL then use a calculated column / in a view etc.

Author

Commented:
I want the value calculated at the time of row creation.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Most Valuable Expert 2012
Distinguished Expert 2018
Commented:
Since hire date is deterministic, I would use a calculated column:
https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-ver15

create table Employees(ID int, Name varchar(10), Hiredate date,
 Seniority as 
    case 
       when datediff(year,hiredate, getdate()) < 1 then 'Newbie'
       when datediff(year,hiredate, getdate()) between 1 and 4 then 'Junior'
       else 'Senior'
     end
);

Open in new window


Working example here:
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=50d03e6b4bfcead484bb30322d5f375a
ste5anSenior Developer

Commented:
My question is, should I put the seniority field as a calculation field? Or should I use the trigger to determine the employee is 'Newbie', 'Junior', or 'Senior'?
None of these, Cause it's a time depended state. Thus it's a separate table where you track the states over time per employee.
Furthermore, a seniority level without department or function is meaningless.

p.s. not all new employee's are hired as newbies.

Author

Commented:
Thank you slightwv (䄆 Netminder).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial