-2

So I have this table. How can I know which Staff has been here the longest?

create table Staff (
    StaffId     char(5) not null,
    StaffName   varchar(20) not null,
    StaffPhone  varchar(13),
    StaffIc     char(14),
    JoinDate    date,
    primary key(StaffId),
    constraint chk_StaffIc check (REGEXP_LIKE(StaffIc,'\d{6}\-\d{2}\-
    \d{4}')),
    constraint chk_StaffPhone check (REGEXP_LIKE(StaffPhone,'^01[0-9]-
    \d{7}|\d{8}$'))
);
Jason Hew
  • 13
  • 4

2 Answers2

1

For the longest, use top 1 and order byJoinDate ascending. Shortest would be descending:

select      top 1
            StaffId,
            StaffName,
            StaffPhone,
            StaffIc,
            JoinDate,
from        Staff
order by    JoinDate asc
PPJN
  • 332
  • 3
  • 13
0

This will tell you how long in days, it could also be month, years, etc. See DATEDIFF()

select top 1 staffid, staffname, DATEDIFF(day,joindate,getdate()) from Staff
order by joindate 

This use GETDATE() to obtain current day

Horaciux
  • 6,322
  • 2
  • 22
  • 41