Lunes anterior y Domingos anteriores fecha basada en la fecha de hoy
Necesito la sintaxis correcta para darme :
- Semanas anteriores Lunes fecha basada en la fecha / hora actual usando
GETDATE()
- Semanas anteriores Domingos fecha basada en la fecha / hora actual usando
GETDATE()
Por lo tanto, basado en la fecha de hoy (14/09/2012) me gustaría lo siguiente:
- Lunes anterior fecha = 03/09/2012
- Domingos anteriores fecha = 09/09/2012
5 answers
Fácil:
--start of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)
--end of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6)
EDITAR :
Lo siguiente se encargará de la cuestión de la fecha del domingo.
DECLARE @input varchar(10)
--SET @input = '9/9/2012' -- simulates a Sunday
SET @input = GETDATE()
--start of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 0)
--end of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 6)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-17 08:51:56
En lugar de usar una opción de caso, también podría hacer esto para obtener el domingo de la semana actual:
SELECT DATEADD(dd, DATEPART(DW,GETDATE())*-1+1, GETDATE())
Para obtener el domingo de la semana anterior, reste 7 días más:
SELECT DATEADD(dd, DATEPART(DW,GETDATE())*-1-6, GETDATE())
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-15 15:38:53
Creo que esta es una solución mucho más limpia:
SELECT
-- 17530101 or 1753-01-01 is the minimum date in SQL Server
DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 7, '17530101') AS [LowerLimit], -- Last Week's Monday
DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 1, '17530101') AS [UpperLimit] -- Last Week's Sunday.
Que se puede usar así en una consulta del mundo real:
SELECT
*
FROM
SomeTable
WHERE
SomeTable.[Date] >= DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 7, '17530101') AND
SomeTable.[Date] <= DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 1, '17530101')
Aquí hay algunas pruebas:
1. Año Bisiesto
Fecha Actual: 2016-02-29 00:00:00.000
Resultados:
LowerLimit UpperLimit
2016-02-22 00:00:00.000 2016-02-28 00:00:00.000
2. La semana pasada fue en año diferente
Fecha Actual: 2016-01-06 00:00:00.000
LowerLimit UpperLimit
2015-12-28 00:00:00.000 2016-01-03 00:00:00.000
3. Límite inferior del mes anterior y límite superior del mes en curso
Fecha Actual: 2016-05-04 00:00:00.000
LowerLimit UpperLimit
2016-04-25 00:00:00.000 2016-05-01 00:00:00.000
4. La fecha actual es Domingo
Fecha Actual: 2016-05-08 00:00:00.000
LowerLimit UpperLimit
2016-04-25 00:00:00.000 2016-05-01 00:00:00.000
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-06 15:46:37
Aún mejor, creo, esto funciona para cualquier fecha, cualquier día de la semana, con cualquier parámetro DateFirst (establecer el primer día de la semana, generalmente 1-Lunes en Francia, por defecto es 7-Domingo).
create function [dbo].[previousWeekDayDate](@anyDate date, @anyWeekDay int)
returns Date
as
begin
return DATEADD(dd, ((DATEPART(dw,@anyDate) + @@DateFirst - @anyWeekDay + 13) % 7) * -1, @anyDate)
end
Funciona para SQL 2008, cree la función y use:
SELECT dbo.previousWeekDayDate(GetDate(),1) --for Monday
SELECT dbo.previousWeekDayDate(GetDate(),7) --for Sunday
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-22 10:23:24
Cabe señalar que el problema con los domingos parece ya no estar presente al menos a partir de MSSQL 2012. Tanto la solución simple
SELECT DATEADD(wk, DATEDIFF(wk, 6, @input), 0)
Y el complejo
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 0)
Devuelve lo mismo para cualquier fecha que haya probado, incluidos los domingos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-20 09:44:26