Meses en Columnas

22/03/2005 - 01:45 por Juan | Informe spam
Hola a tod@s

Necesito saber como puedo tabular la informacion de una columna de tipo
datetime, de tal forma que me quede, las transacciones son mensuales al
ultimo dia del mes.

Enero Febrero ... Dic
31/01/2005 28/02/2005 31/12/2005
31/01/2006 28/02/2006 31/12/2006
31/01/2007 28/02/2007 31/12/2007

Preguntas similare

Leer las respuestas

#1 MAXI
22/03/2005 - 02:00 | Informe spam
Hola, lo podrias hacer con CASE y el numero del mes

veamos un simple ejemplo


create table #pepe (fecha datetime)

insert into #pepe values ('20050101')
insert into #pepe values ('20040101')
insert into #pepe values ('20050201')
insert into #pepe values ('20050301')
insert into #pepe values ('20040301')
insert into #pepe values ('20030301')

select CASE WHEN DATEPART(MM,FECHA)=1 THEN FECHA END AS ENERO,
CASE WHEN DATEPART(MM,FECHA)=2 THEN FECHA END AS FEBRERO,
CASE WHEN DATEPART(MM,FECHA)=3 THEN FECHA END AS MARZO,
CASE WHEN DATEPART(MM,FECHA)=4 THEN FECHA END AS ABRIL
FROM #PEPE

==
Un abrazo



Maxi
Buenos Aires - Argentina
Desarrollador .NET 3 Estrellas
Microsoft User Group (MUG)



"Juan" escribió en el mensaje
news:
Hola a

Necesito saber como puedo tabular la informacion de una columna de tipo
datetime, de tal forma que me quede, las transacciones son mensuales al
ultimo dia del mes.

Enero Febrero ... Dic
31/01/2005 28/02/2005 31/12/2005
31/01/2006 28/02/2006 31/12/2006
31/01/2007 28/02/2007 31/12/2007
Respuesta Responder a este mensaje
#2 Alejandro Mesa
22/03/2005 - 02:11 | Informe spam
Juan,

Aqui tienes un ejemplo y un link a un articulo que habla sobre el tema.

Ejemplo:

use northwind
go

create table t (
colA datetime
)
go

declare @month int
declare @year int

set @year = 2000

while @year <= 2010
begin
set @month = 1

while @month <= 12
begin
insert into t values(dateadd(day, -1, dateadd(month, 1, ltrim(@year) +
right('0' + ltrim(@month), 2) + '01')))
set @month = @month + 1
end

set @year = @year + 1
end
go

select
max(case when month(colA) = 1 then convert(varchar(10), colA, 103) end) as
[Ene],
max(case when month(colA) = 2 then convert(varchar(10), colA, 103) end) as
[Feb],
max(case when month(colA) = 3 then convert(varchar(10), colA, 103) end) as
[Mar],
max(case when month(colA) = 4 then convert(varchar(10), colA, 103) end) as
[Abr],
max(case when month(colA) = 5 then convert(varchar(10), colA, 103) end) as
[May],
max(case when month(colA) = 6 then convert(varchar(10), colA, 103) end) as
[Jun],
max(case when month(colA) = 7 then convert(varchar(10), colA, 103) end) as
[Jul],
max(case when month(colA) = 8 then convert(varchar(10), colA, 103) end) as
[Ago],
max(case when month(colA) = 9 then convert(varchar(10), colA, 103) end) as
[Sep],
max(case when month(colA) = 10 then convert(varchar(10), colA, 103) end) as
[Oct],
max(case when month(colA) = 11 then convert(varchar(10), colA, 103) end) as
[Nov],
max(case when month(colA) = 12 then convert(varchar(10), colA, 103) end) as
[Dic]
from
t
group by
year(cola)
order by
year(colA)
go

drop table t
go

HOW TO: Rotate a Table in SQL Server
http://support.microsoft.com/defaul...US;q175574


AMB

"Juan" wrote:

Hola a

Necesito saber como puedo tabular la informacion de una columna de tipo
datetime, de tal forma que me quede, las transacciones son mensuales al
ultimo dia del mes.

Enero Febrero ... Dic
31/01/2005 28/02/2005 31/12/2005
31/01/2006 28/02/2006 31/12/2006
31/01/2007 28/02/2007 31/12/2007
Respuesta Responder a este mensaje
#3 Juan
22/03/2005 - 16:21 | Informe spam
Muchas gracias a los 2 fueron respuestas muy útiles...

Saludos...

"Alejandro Mesa" wrote:

Juan,

Aqui tienes un ejemplo y un link a un articulo que habla sobre el tema.

Ejemplo:

use northwind
go

create table t (
colA datetime
)
go

declare @month int
declare @year int

set @year = 2000

while @year <= 2010
begin
set @month = 1

while @month <= 12
begin
insert into t values(dateadd(day, -1, dateadd(month, 1, ltrim(@year) +
right('0' + ltrim(@month), 2) + '01')))
set @month = @month + 1
end

set @year = @year + 1
end
go

select
max(case when month(colA) = 1 then convert(varchar(10), colA, 103) end) as
[Ene],
max(case when month(colA) = 2 then convert(varchar(10), colA, 103) end) as
[Feb],
max(case when month(colA) = 3 then convert(varchar(10), colA, 103) end) as
[Mar],
max(case when month(colA) = 4 then convert(varchar(10), colA, 103) end) as
[Abr],
max(case when month(colA) = 5 then convert(varchar(10), colA, 103) end) as
[May],
max(case when month(colA) = 6 then convert(varchar(10), colA, 103) end) as
[Jun],
max(case when month(colA) = 7 then convert(varchar(10), colA, 103) end) as
[Jul],
max(case when month(colA) = 8 then convert(varchar(10), colA, 103) end) as
[Ago],
max(case when month(colA) = 9 then convert(varchar(10), colA, 103) end) as
[Sep],
max(case when month(colA) = 10 then convert(varchar(10), colA, 103) end) as
[Oct],
max(case when month(colA) = 11 then convert(varchar(10), colA, 103) end) as
[Nov],
max(case when month(colA) = 12 then convert(varchar(10), colA, 103) end) as
[Dic]
from
t
group by
year(cola)
order by
year(colA)
go

drop table t
go

HOW TO: Rotate a Table in SQL Server
http://support.microsoft.com/defaul...US;q175574


AMB

"Juan" wrote:

> Hola a
>
> Necesito saber como puedo tabular la informacion de una columna de tipo
> datetime, de tal forma que me quede, las transacciones son mensuales al
> ultimo dia del mes.
>
> Enero Febrero ... Dic
> 31/01/2005 28/02/2005 31/12/2005
> 31/01/2006 28/02/2006 31/12/2006
> 31/01/2007 28/02/2007 31/12/2007
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida