1

「yy年mm月dd日」の形式で従業員の経験を調べるクエリが必要です。

SELECT EMPID, EMPNAME, DEPARTMENT, DESIGNATION, DATEDIFF(YEAR, DOJ, GETDATE()) AS EXPERIENCE, 
       EMPSTATUS AS JOB_STATUS 
FROM EMPLOYEE

DOJ - 従業員の「入社日」を保存するためのデータベース内のフィールド。これは、経験年数のみを返すクエリです。それを変更する方法は?

4

6 に答える 6

1
SELECT 
    EMPID, EMPNAME, DEPARTMENT, DESIGNATION, 

    convert(varchar(3),DATEDIFF(MONTH, DOJ, GETDATE())/12) +' years '+
    convert(varchar(2),DATEDIFF(MONTH, DOJ, GETDATE()) % 12)+ ' months' 
    AS EXPERIENCE, 

    EMPSTATUS AS JOB_STATUS 
FROM EMPLOYEE
于 2012-11-27T08:24:40.727 に答える
0

テスト用のSQL Fiddle

SELECT EMPID, EMPNAME, DEPARTMENT, DESIGNATION,
       cast(floor(experience / 365) as varchar) + ' years ' +
       cast(floor(experience % 365 / 30) as varchar) + ' months ' +
       cast(experience % 30 as varchar) + ' days' as experience,
       EMPSTATUS AS JOB_STATUS 
FROM (select *, datediff(DAY, doj, getdate()) as experience
      from employee) t
于 2012-11-27T08:30:08.820 に答える
-1
SELECT convert(varchar(3),DATEDIFF(MONTH, '2015-01-01', GETDATE())/12) +' years '+
    convert(varchar(2),DATEDIFF(DD, '2016-09-21', GETDATE()) % 12)+ ' months' 
    AS EXPERIENCE 
于 2016-09-26T10:38:11.620 に答える