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  
  •  Recovery disk No.2 needed for a Sony Vaio PCG-FR102/FR105
  •  Insert Data from a table and also add a date/time the data was added in a column called DateRcv
  •  Hibernate Mapping - Stored Proc result from multiple tables
  •  Replace Windows 2000 Server with 2003 server
  •  Is there a way to export a SharePoint group to an excel file?
  •  Infopath Multiple-Selection List Box: linked to access and list populated from Access table
  •  Can I make Sharepoint sites or folders invisible to users without rights?
  •  Moving an image along side the Mouse Pointer moves
  •  microsoft access query criteria to retrieve specific records based on a reference number and part number
  •  The Microsoft Jet database engine could not find the object 'Databases'.  Make sure the object exists and that you spell its name and the path name correctly.
  •  
    programming4us programming4us