Question : last month and week data set

Hello
How can I query on a date field for last full weeks worth of data and if required last full months worth of data - using microsoft sql server

Regards

Answer : last month and week data set

philsivyer,

Something like this, then...

/* Month */

DECLARE @Start datetime, @End datetime

SET @Start = DATEADD(m, -1, DATEADD(d, 1 - DAY(CONVERT(datetime, CONVERT(varchar, GETDATE(), 102), 102)), CONVERT(datetime, CONVERT(varchar, GETDATE(), 102), 102)))
SET @End = DATEADD(d, 1 - DAY(CONVERT(datetime, CONVERT(varchar, GETDATE(), 102), 102)), CONVERT(datetime, CONVERT(varchar, GETDATE(), 102), 102))

SELECT col1, col2, col3, ..., colN
FROM SomeTable
WHERE DateCol >= @Start And DateCol < @End



/* Week */

DECLARE @Start datetime, @End datetime

SET @Start = CONVERT(datetime, CONVERT(varchar, GETDATE(), 102), 102)
SET @Start = DATEADD(d, CASE DATENAME(dw, @Start)
      WHEN 'Sunday' THEN -7
      WHEN 'Monday' THEN -8
      WHEN 'Tuesday' THEN -9
-- ...
      WHEN 'Saturday' THEN -13 END, @Start)
SET @End = DATEADD(d, 7, @Start)

SELECT col1, col2, col3, ..., colN
FROM SomeTable
WHERE DateCol >= @Start And DateCol < @End



Patrick
Random Solutions  
 
programming4us programming4us