从sql中的表中减去两个日期

时间:2015-06-05 07:58:32

标签: sql oracle date plsql date-difference

我有一张桌子,上面有一些员工的出生日期。如何找到两名员工之间的年龄差异?

2 个答案:

答案 0 :(得分:1)

您可以尝试以下两个日期之间的日期,月份和年份。

DECLARE

dob1 date := TO_DATE('11-17-2015','MM-DD-YYYY'); -- change your dates here
dob2 date := TO_DATE('11-18-2012','MM-DD-YYYY');
v_years varchar2(100);
v_months varchar2(100);
v_days varchar2(100);

BEGIN

select (abs(dob1 - dob2)) into v_days from dual;
DBMS_output.put_line('DAYS   : ' || v_days);

select floor(abs(dob1 - dob2)/365) into v_years from dual;
DBMS_output.put_line('YEARS  : ' || v_years);

select floor(abs(dob1 - dob2)/365*12) into v_months from dual;
DBMS_output.put_line('MONTHS : ' || v_months);


END;
/

<强>输出:

DAYS:1094
年份:2
月:35

PL / SQL过程成功完成。

答案 1 :(得分:0)

在Oracle中减去两个opr_operator_ods.Close; opr_operator_ods.SetVariable('DATEFROM', opr_datefrom_dtp.Date); opr_operator_ods.SetVariable('DATETO', opr_dateto_dtp.Date); opr_operator_ods.SetVariable('TIMEFROM', opr_timefrom_dtp.Text); opr_operator_ods.SetVariable('TIMETO', opr_timeto_dtp.Text); opr_operator_ods.Open; if opr_operator_ods.RecordCount > 0 then begin while not opr_operator_ods.Eof do begin opr_operatorcount_ods.Close; opr_operatorcount_ods.SetVariable('DATEFROM', opr_datefrom_dtp.Date); opr_operatorcount_ods.SetVariable('DATETO', opr_dateto_dtp.Date); opr_operatorcount_ods.SetVariable('TIMEFROM', opr_timefrom_dtp.Text); opr_operatorcount_ods.SetVariable('TIMETO', opr_timeto_dtp.Text); opr_operatorcount_ods.SetVariable('OPERATOR', opr_operator_ods.FieldByName('opr_code').AsString); opr_operatorcount_ods.Open; while not opr_operatorcount_ods.Eof do begin frxReport1.PrepareReport(false); opr_operatorcount_ods.Next; end; frxReport1.PrepareReport(true); opr_operator_ods.Next; end; DecodeDate(opr_datefrom_dtp.Date, tyear, tmonth, tday); StartDate := '''' + IntToStr(tday) + '/' + IntToStr(tmonth) + '/' + IntToStr(tyear) + ''''; DecodeDate(opr_dateto_dtp.Date, tyear, tmonth, tday); EndDate := '''' + IntToStr(tday) + '/' + IntToStr(tmonth) + '/' + IntToStr(tyear) + ''''; frxReport1.Variables['StartDate'] := StartDate; frxReport1.Variables['EndDate'] := EndDate; //frxReport1.PrepareReport(True); frxReport1.ShowPreparedReport; 列将导致天数的差异,您可以将其除以365以获得多年的差异:

date