Está en la página 1de 3

--------------------------------------------------------------------------Consultas realizadas a la base de datos del sistema------------------------------------------------------------------------------Manejo de Fechas

--Tabla por defecto dual


describe dual
--Mostrar la fecha del sistema
select sysdate As FechaActual form dual;
--Sumar dias a una fecha
select (sysdate + 3) As FechaProxima from dual;
--Restar dias una fecha
select (sysdate - 3) As FechaAnterior from dual;
--Sumar meses a una fecha
select add_months(sysdate,3) As FechaProxima from dual;
--Restar meses a una fecha
select add_months(sysdate,-3) As FechaAnterior from dual;
--Sumar tres aos a la fecha Actual
select add_months(sysdate,3*12) As FechaProxima from dual;
--Restar tres aos a la fecha Actual
select add_months(sysdate,3*-12) As FechaAnterior from dual;
--Sumar meses y aos
select sysdate + numtoyminterval(6,'MONTH') AS NuevaFecha from dual;
select sysdate + numtoyminterval(6,'YEAR') Aa NuevaFecha from dual;
--Obterner el aos de una fecha
select to_char (sysdate, 'YYYY') As Ao from dual;
--Para solo 2 digitos de un ao
select to_char (sysdate, 'YY') As Ano from dual;
--Obtener el mes de una Fecha
select to_char (sysdate, 'MM') As Ao from dual;
--Obtener la abreviatura de los meses
select to_char (sysdate, 'MON') As Mes from dual;
select to_char (sysdate,'MONTH') As Mes from dual;
--Obtener el dia de una fecha
select to_char (sysdate, 'DD') as Dia from dual;
select to_char (sysdate, 'DAY') as Dia form dual;
--Obtener el dia , mes y el ao como numeros
select extract(day from sysdate) as Dia,
extract(month from sysdate) as Mes,
extract(Year from sysdate) as Ao from dual;
--------------------------------------------------------------------------------

---------------------------------Crear la Tabla Fechas--------------------------------------------------------------------------------------------------------------------create table Mod_Fech


(
IdFecha int primary key not null,
Fini date not null,
Ffin date not null
);
insert into Mod_Fech (IdFecha, Fini, Ffin) values (1, to_date('01/10/2013'), sys
date);
select * from Mod_Fech;
--Obtener los dias transcurrdiso entre dos fechas
select (Ffin - Fini) As DiasTranscurridos from Mod_Fech;
select trunc(Ffin - Fini) As DiasTranscurridos from Mod_Fech;
update Mod_Fech set Fini = to_date('01/01/2013')
where IdFecha = 1;
--Obtener los meses trancusridos entre dos fechas
select trunc((to_char (Ffin, 'MM')) - (to_char (Fini, 'MM')))
As MesesTranscurridos from Mod_Fech;
--Obtener las semanas trascurridas entre dos fechas
select trunc((Ffin - Fini)/7)
As SemanasTranscurridos from Mod_Fech;
--Obtener el ultimo dia de una fecha
select last_day(sysdate) as UltimaFecha from dual;
select last_day(Fini) as FechaIncicial,
last_day(Ffin) as FechaFinal from Fechas;
update Fechas set fini = to_date('01/02/2013')
where idevha = 1;
--------------------Horas------------------ Obtener la hora actual del sistema
select to_char(sysdate,'HH:MI:SS') AS HoraActual from dual;
select to_char(systimestamp,'HH:MI:SS') AS HoraActual from dual;
select to_char(sysdate,'HH24:MI:SS') AS HoraActual from dual;
select to_char(sysdate, 'HH:MI:SS AM') AS HoraActual from dual;
--Obtener las horas
select to_char(sysdate, 'HH') AS Horas from dual;
select to_char(systimestamp, 'HH') AS Horas from dual;
--Obtener los minutos
select to_char(sysdate, 'MI') AS Horas from dual;
--Obetener los segundos
select to_char(sysdate, 'SS') AS Horas from dual;

select systimestamp as FechaHora from dual;


--Obtener las horas minutos y segundos como numeros
select extract(hour from systimestamp ) as Horas,
extract (minute from systimestamp) as Minutos,
extract (second from systimestamp) as Segundos
from dual;
--Sumar Horas
select systimestamp + numtodsinterval(60, 'HOUR') AS Hora from dual;
--Minutos Horas
select systimestamp + numtodsinterval(60, 'MINUTE') AS Hora from dual;
--Minutos Segundos
select systimestamp + numtodsinterval(60, 'SECOND') AS Hora from dual;
select to_char(systimestamp + numtodsinterval(5,'HOUR'), 'HH24:MI:SS') AS Hora
from dual;
--obtener las Horas transcurridas entre 2 horas
--opcional visualizar en el formato HH:MI:SS