Finding Yesterday in SQL and C#

4.78 (9 votes)

Here's something that comes up often in forums - How To Find Yesterday in SQL or C#. Piece of cake, if you know how, but tricky if you don't. And especially tricky to get the right value if you are not clear on the requirement.

The current time (in C#), as I type, is 12/07/2010 19:21:36. That's achieved by using Console.WriteLine(DateTime.Now); on a box with UK regional settings. According to SQL Server, it's now 2010-07-12 19:21:36.957. That's obtained through executing the following query: SELECT GetDate(). So we know that DateTime.Now and GetDate() are the ways to get the current date and time in C# and SQL respectively.

In C#, there is an AddDays() method that takes an integer. That integer can be negative, so the following will obtain the date and time for yesterday: Console.WriteLine(DateTime.Now.AddDays(-1));. SQL has a similar function: DateAdd(). This takes an interval or datepart as they are known, an integer (which can also be negative) and a datetime. There are a number of accepted values for the datepart argument, which can be found here. To get the same result as the preceding C# code, you would simply use SELECT DATEADD(d, -1, GetDate()).

So far so good, if all you need is the date and time for 24 hours ago (11/07/2010 19:21:36). However, and here's the rub - often, you might want to obtain all events that happened yesterday (or the day before today) from a collection or a database table. If you were to use the preceding SQL example in a statement like this:

SELECT * FROM Table1 WHERE EventTime < DATEADD(d, -1, GetDate())

you will get all items that have an EventTime value before yesterday at 19:21:36. All items that occured after that time will not be included. Not quite what you would expect, maybe. The same problem exists if you are querying a collection of C# objects using DateTime.Now.AddDays(-1) as the basis for the comparison. Try this example code:

var times = new List<DateTime>();
for (var i = 1; i <= 48; i++)
{
  times.Add(DateTime.Now.AddHours(-i));
}


foreach (var t in times.Where(t => t < DateTime.Now.AddDays(-1)))
{
  Console.WriteLine(t);
}

Console.ReadLine();

All it does is build a List<DateTime> with items 1 hour apart, going backwards from now. However, it will only select those items that have a value prior to 19:21:36 yesterday, which leave some events from yesterday still untroubled - ie those between 19:21:36 and midnight.

It's the pesky time part that gets in the way, so that needs to be changed to 00:00:00 or midnight. Previously, this article showed how to do that with C#, until Steve contributed his comment below, pointing out that DateTime.Now.Date gives us exactly that. However, in SQL, the solution is to change the datetime to a string:

[C#]
var yesterday = DateTime.Now.Date;
foreach (var t in times.Where(t => t < yesterday))
{
  Console.WriteLine(t);
}
[SQL]
SELECT * FROM Table1 WHERE EventTime < CONVERT(varchar(10), GETDATE(), 101)

As someone once said: Hope This Helps.

 

Date Posted:
Last Updated:
Posted by:
Total Views to date: 17791

4 Comments

- Mike

Thanks man!, actually used that snippet concept today lol

- Steve

what about:

var yesterday = DateTime.Now.Date;

- Mike

@Steve,

Yes - that will do it :o)

- dotnetcoder

Simple and very informative!

Recent Comments

Praveen 12/02/2016 14:22
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
Nicely written article, just what I wanted to get me started, I am going to start working on a this...

Whitney W. 11/02/2016 15:37
In response to Adding A Controller
I am really new to everything and just started programming. I really need help in my project since I...

Fredrik 11/02/2016 13:10
In response to Request.Form Is Empty When Posting To ASPX Page
It worked. Thank you!...

David Valdez 11/02/2016 03:08
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Muchas, muchas, gracias. Thank you so much from Dominican Republic....

Zahid 10/02/2016 00:42
In response to How To Send Email In ASP.NET MVC
Hello Sir, Great post. Just a quick question, is it possible if we can ask a client to save in an...

Al Wilton 07/02/2016 03:11
In response to Windows Authentication With ASP.NET Web Pages
I've been using this advise for quite a while. Today I was setting up a .NET 4.6.1 site and it drove...

Anders 06/02/2016 15:38
In response to iTextSharp - Working with Fonts
Thanks a lot for this excellent series on iTextSharp....

J_R 06/02/2016 02:45
In response to Simple File Download Protection with ASP.NET
Mike, Thank you for taking the time to write this. It really helped me - though I could not out a...

Nemat 04/02/2016 16:24
In response to Solved - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine
Installing Microsoft Access Database Engine 2010 64 bit helped me. Thanks A lot!...

Bill Barbour 03/02/2016 18:34
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Wonderful example. I have it all working. I would like to add the image to each row of the index you...