Consulta dia mes anterior dependiendo dia actual
Publicado por kinetic_brain (4 intervenciones) el 04/08/2009 16:46:30
Hola foro me explico
tengo una consulta IF que pregunta si es dia lunes o día 1 de la semana y pregunto por 1, 2 y 3 dias hacia atras y caso contrario si no es dia 1 (lunes) que solo tome 1 dia hacia atras, el problema lo tengo, por ejemplo ayer 3 de agosto la consulta solo tomo los dias 1 y 2 de agosto pero no el 31 de julio. por favor.
if (select datepart(dw,getdate()))='1'
begin
insert into cambio_1
select distinct Tele_New, Fecha_Emision, '' as Fecha_Alta, '' as Dif_Fecha''
from cambio_m3
where
((fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-3) as char)),2))
or (fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-2) as char)),2))
or (fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-1) as char)),2)))
end
else
begin
insert into cambio_1
select distinct Tele_New, Fecha_Emision, '' as Fecha_Alta, '' as Dif_Fecha''
from cambio_m3
where
(ltrim(rtrim(fecha_emision)) = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-1) as char)),2))
end
tengo una consulta IF que pregunta si es dia lunes o día 1 de la semana y pregunto por 1, 2 y 3 dias hacia atras y caso contrario si no es dia 1 (lunes) que solo tome 1 dia hacia atras, el problema lo tengo, por ejemplo ayer 3 de agosto la consulta solo tomo los dias 1 y 2 de agosto pero no el 31 de julio. por favor.
if (select datepart(dw,getdate()))='1'
begin
insert into cambio_1
select distinct Tele_New, Fecha_Emision, '' as Fecha_Alta, '' as Dif_Fecha''
from cambio_m3
where
((fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-3) as char)),2))
or (fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-2) as char)),2))
or (fecha_emision = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-1) as char)),2)))
end
else
begin
insert into cambio_1
select distinct Tele_New, Fecha_Emision, '' as Fecha_Alta, '' as Dif_Fecha''
from cambio_m3
where
(ltrim(rtrim(fecha_emision)) = right('0000'+rtrim(cast(year(getdate()-1) as char)),4)+right('00'+rtrim(cast(month(getdate()-1) as char)),2)+right('00'+rtrim(cast(day(getdate()-1) as char)),2))
end
Valora esta pregunta
0