2013-10-27 156 views
0

我遇到了計算教職員工聘用年數的問題。 (當前時間 - 教師聘請日期)在SQL中計算日期

我想使用這個,但是我總是收到錯誤,DATEDIFF是一個無效的標識符。請幫忙。

SELECT FAC_FN , DATEDIFF ('CURDATE()' , 'FAC_HIRE DATE') 
FROM FACULTY; 

--CREATING TABLE FACULTY 

CREATE TABLE FACULTY 
(
FAC_ID NUMBER (4) CONSTRAINT FAC_ID_PK PRIMARY KEY, 
FAC_FN VARCHAR2 (15), 
FAC_LN VARCHAR2 (15), 
FAC_DEPT VARCHAR2 (10), 
FAC_RANK VARCHAR2 (10), 
FAC_HIRE_DATE DATE, 
FAC_SALARY NUMBER (7), 
FAC_SUPERVISOR NUMBER (4) 
); 

--INSERTING RECORDS INTO FACULTY TABLE 

INSERT INTO FACULTY VALUES (9001 , 'Leonard' , 'Vince' , 'IS' , 'ASST' , TO_DATE('12-APR-1997','DD-MON-YYYY') , 67000 , 9003); 

INSERT INTO FACULTY VALUES (9002 , 'Victor' , 'Strong' , 'CSCI' , 'ASSO' , TO_DATE('8-AUG-1999','DD-MM-YYYY') , 70000 , 9003); 

INSERT INTO FACULTY VALUES (9003 , 'Nicki' , 'Colan' , 'IS' , 'PROF' , TO_DATE('20-AUG-1981','DD-MM-YYYY') , 75000, 9010); 

INSERT INTO FACULTY VALUES (9004 , 'Fred' , 'Wells' , 'ACCT' , 'ASST' , TO_DATE('28-AUG-1996','DD-MM-YYYY'), 60000, 9010); 

INSERT INTO FACULTY VALUES (9010 , 'Chris' , 'Macon' , 'ACCT' , 'ASST' , TO_DATE('4-AUG-1980','DD-MM-YYYY') , 75000 , ''); 
+0

我正在使用mySQL – user2901565

回答

1

刪除引號(他們所造成的MySQL來解析函數的參數爲​​字符串文字):也

SELECT FAC_FN, DATEDIFF(CURDATE(), FAC_HIRE_DATE) FROM FACULTY 

注意DATEDIFF()返回天數的結果;爲了獲得年份的差異,您可能希望改爲使用TIMESTAMPDIFF()