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 回答
1

将 Emp_joiningDate 视为列

SELECT DATEDIFF(year, Emp_joiningDate, GETDATE()) AS Years,

   DATEDIFF(month, Emp_joiningDate, GETDATE()) - DATEDIFF(year, '1/2/1999', GETDATE()) * 12 AS Months,

    DATEDIFF(day, Emp_joiningDate, getdate())- DATEDIFF(month, '1/2/1999', 

            GETDATE()) - DATEDIFF(year, '1/2/1999', GETDATE()) * 365 as Days
于 2014-06-24T08:30:25.647 回答
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 回答
0
DECLARE @FromDate DATETIME = '2013-12-01 23:59:59.000', 
        @ToDate   DATETIME = '2016-08-30 00:00:00.000',
         DECLARE @MONTHS INT

        SET @Months = DATEDIFF(MM,@FROMDATE,@TODATE)


        IF (DATEPART(MM,@FromDate) <=  (DATEPART(MM,@TODATE))+1)
            BEGIN
                    SELECT CAST(DATEDIFF(YY,@FROMDATE,@TODATE) AS VARCHAR(5)) + ' Years '+ CAST( (DATEPART(MM,@TODATE)-DATEPART(MM,@FROMDATE)) AS VARCHAR(5))+1 +' Month'
            END 
        ELSE
            BEGIN
                    SELECT CAST(DATEDIFF(YY,@FROMDATE,@TODATE)-1 AS VARCHAR(5)) +' Years '+ CAST(12-(DATEPART(MM,@FROMDATE)) + DATEPART(MM,@TODATE)+1  AS VARCHAR(5) ) +' Month'
            END 
于 2016-10-21T10:49:52.857 回答
0

创建 FUNCTION [dbo].[fn_getEmployePeriod] (@dateofbirth DATETIME) 返回 VARCHAR(100) 开始声明 @currentdatetime DATETIME;声明@years INT;声明@months INT;声明@days INT;声明@currentMonthdays INT;声明@result VARCHAR(100); SET @currentdatetime = GETDATE();--当前日期时间

    IF ( @dateofbirth <= GETDATE() )
        BEGIN
            SELECT  @years = DATEDIFF(YEAR, @dateofbirth, @currentdatetime); -- To find Years
            SELECT  @months = DATEDIFF(MONTH, @dateofbirth,
                                       @currentdatetime) - ( DATEDIFF(YEAR,
                                                          @dateofbirth,
                                                          @currentdatetime)
                                                          * 12 );
            SELECT  @days = DATEPART(d, @currentdatetime) - DATEPART(d,
                                                          @dateofbirth);-- To Find Days
            SELECT  @currentMonthdays = ( SELECT    DAY(DATEADD(DD, -1,
                                                          DATEADD(mm,
                                                          DATEDIFF(mm, 0,
                                                          GETDATE()), 0)))
                                        );
            IF ( @months < 0 )
                BEGIN
                    SET @months = 12 + @months;
                    SET @years = @years - 1;
                END;
            IF ( @days < 0 )
                BEGIN
                    SET @days = @currentMonthdays + @days;
                     IF(@months<>0)
                    begin
                    SET @months = @months - 1;
                    END
                    ELSE
                    BEGIN
                    SET @years = @years - 1;
                    SET @months = 11;
                    end
                END;

-- 查找月份

            SET @result = CAST(@years AS VARCHAR(3)) + ' years,   '
                + CAST(@months AS VARCHAR(3)) + ' months,   '
                + CAST(@days AS VARCHAR(3)) + ' days'; 

        END;
    ELSE
        BEGIN
            SET @result = 'Invaild date of birth';
        END;
    RETURN @result;
END;
于 2019-01-06T05:31:13.907 回答
-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 回答