February 26, 2010 at 2:32 am
Hi there
Please help
my client would like a list of employees showing how long an employee has been in their current position/job.
I don't have an idea of where to start with the query.
February 26, 2010 at 2:35 am
Please post table structure for dates
Vaibhav K Tiwari
To walk fast walk alone
To walk far walk together
February 26, 2010 at 2:42 am
Go to Books Online (Books online is the documentation/help available with SQL Server), look for DATEDIFF function.
Partial Syntax of the function is:
DATEDIFF( Interval, Date1, Date2 )
--Ramesh
February 26, 2010 at 2:46 am
Sorry about that
Staffno name position posid headcount period
123 john developer d24 Newhire 200911
321 Doe Analyst A15 TransferIn 200906
NULL NULL Web W33 Vacancy 200910
So I would like to know how long each of these people has been in their current positions, was it from date of emplyment or since they transfered from different positions
February 26, 2010 at 2:58 am
What would be the expected result for the (very limited) sample data?
February 26, 2010 at 8:57 pm
Trybbe (2/26/2010)
Sorry about thatStaffno name position posid headcount period
123 john developer d24 Newhire 200911
321 Doe Analyst A15 TransferIn 200906
NULL NULL Web W33 Vacancy 200910
So I would like to know how long each of these people has been in their current positions, was it from date of emplyment or since they transfered from different positions
You don't have to if you don't want to but to get tested code answers really quickly, take a look at the first link in my signature line below. A lot of us won't post code unless we've tested it and we just don't have time to make your data "readily consumable" even if it's only 3 lines. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
February 26, 2010 at 11:09 pm
A guess:
DECLARE @Employee
TABLE (
row_id INTEGER IDENTITY PRIMARY KEY,
staff_no INTEGER NULL,
name NVARCHAR(50) NULL,
position NVARCHAR(50) NOT NULL,
pos_id CHAR(3) NOT NULL,
headcount VARCHAR(10) NOT NULL,
period CHAR(6) NOT NULL
);
INSERT @Employee
(
staff_no,
name,
position,
pos_id,
headcount,
period
)
VALUES (
123,
N'John',
N'Developer',
'D24',
'Newhire',
'200911'
);
INSERT @Employee
(
staff_no,
name,
position,
pos_id,
headcount,
period
)
VALUES (
321,
N'Doe',
N'Analyst',
'A15',
'TransferIn',
'200906'
);
INSERT @Employee
(
staff_no,
name,
position,
pos_id,
headcount,
period
)
VALUES (
NULL,
NULL,
N'Web',
'W33',
'Vacancy',
'200910'
);
SELECT staff_no,
name,
position,
pos_id,
headcount,
period,
tenure_days = DATEDIFF(DAY, CONVERT(DATETIME, period + '01', 112), CURRENT_TIMESTAMP)
FROM @Employee
WHERE staff_no IS NOT NULL
ORDER BY
name ASC;
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply