Scripts para la Tabla de Tiempo

21 01 2008

Juan Ernesto Ramos Hernández nos envía un script para llenar la tabla de tiempo. Juan Ernesto reúne en una sola tabla la parte fecha y la parte hora de las dimensiones. Esperamos les sea de gran utilidad. Para contactarlo escribele a: JuanErnestoRamozHernandez

/*
  Llena la tabla de hora por minutos
  Autor: Juan Ernesto Ramos Hernández
  SQLServer 2005
*/

if exists(select name from sysobjects where name = 'Dim_HoraDia')
      DROP TABLE Dim_HoraDia
GO

CREATE TABLE Dim_HoraDia (
      HoraDia_id            int NOT NULL, -- Llave subrogada
      Minutos				int NOT NULL, -- Llave natural
      Hora12                varchar(5) NOT NULL, -- formato de hora de 12
      Hora24				varchar(5) NOT NULL, -- formato de hora de 24
      Meridiano             varchar(2) NOT NULL, -- AM o PM
)
GO

ALTER TABLE Dim_HoraDia
       ADD PRIMARY KEY (HoraDia_Id)
GO

SET LANGUAGE spanish
SET NOCOUNT ON

declare @hcount int
declare @fecha smalldatetime
declare @fechaMod smalldatetime
declare @Hora12 varchar(5)
declare @Hora24 varchar(5)

set @hcount=0
set @fecha = '1900-01-01 00:00' -- se usa la variable @fecha para sumarle en cada vuelta del ciclo un minuto

while @hcount < 1440 --1440 son el numero de minutos que tiene un dia completo
begin
  set @fechaMod = dateadd(mi,@hcount,@fecha)
  insert into Dim_HoraDia ( HoraDia_ID, Minutos, Hora12, Hora24, Meridiano)
  select case
           when datepart(hh,@fechaMod) < 10 and datepart(mi,@fechaMod) = 10 and datepart(mi,@fechaMod) < 10 then '1' + datename(hh,@fechaMod) + '0' + datename(mi,@fechaMod)
           when datepart(hh,@fechaMod) = 10 then '10' + datename(hh,@fechaMod) + datename(mi,@fechaMod)
           when datepart(hh,@fechaMod) >= 10 and datepart(mi,@fechaMod) >= 10 then '1' + datename(hh,@fechaMod) +  datename(mi,@fechaMod)
          end     as HoraDia_Id,
          @hcount as minutos,
          case
            when @hcount = 780 then convert(varchar(5),dateadd(hh,-12,@fechaMod),8)
          end     as Hora12,
          convert(varchar(5),@fechaMod,8) as Hora24,
          case
           when @hcount =  720 then 'PM'
          end as Meridiano

   set @hcount = @hcount + 1
end 

Advertisement

Acciones

Información

Deja un comentario

Fill in your details below or click an icon to log in:

Gravatar
Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s




Seguir

Get every new post delivered to your Inbox.