Search code examples
visual-studio-2010sql-server-2008date-comparison

sql query - variables - date - visual studio 210


Trying to search between two dates to only get certain rows. A 7 day search.

   @date = DateTime.Today
   @date2 = //need it to be the prior 7 days

SelectCommand = "SELECT [DateReceived], [DeviceLevel] FROM [TBLReadings] WHERE [DateReceived=@date] <= [DateReceived=@date2] ORDER BY [DateReceived] DESC;

This is wrong but I hope it explain what I am trying to do - only ever used PHP and MySQL.


Solution

  • if you are trying to search between 2 dates, you can use between

    the query will be something like

    select [dateRecieved], [DeviceLevel] from YourTable where DateReceived between @date and @date2 ORDER BY [DateReceived] DESC
    

    in c#, to get 7 days you can use Datetime.Now.AddDays(7)

    http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx

    sorry for the bad english!

    edit: that's only the query, use it as a string in the select command