How to use DateTime in a SqlFilter with .NET

Have you been wondering how to use DateTime inside of SqlFilters, with your Topics/Subscriptions? Have you also noticed that this doesn't seem to work?

var message = new BrokeredMessage();
message.Properties["datetime"] = DateTime.Now;

var sqlFilter = new SqlFilter("datetime > '2016-06-06'");

That's because you explicitly need to add the DateTime as a parameter, like so:

var filter = new SqlFilter(" datetime >= @datetime");
filter.Parameters.Add("@datetime", DateTime.Parse("2016-06-06"));

In order to make this more searchable in the future, I have also added a corresponding StackOverflow question:

Comments (3)
  1. Sean Feldman says:

    Great info John, thank you.
    Do you think it would be good to have this kind of info at as well?

    1. Thanks for the note Sean! We added an example using dates to the MSDN page.

  2. Sean Feldman says:

    One more thing: If you have a single filter like that, it’s ok. If you have multiple filters, the current limitation of parameters/subscriptions makes it almost impossible to maintain. Parameter values are not discoverable. Looking at a filter once it’s created, one cannot not know what’t the value that parameter represents. I’ve raised an issue about this. Should be somewhere on the backlog (hopefully) 🙂

Comments are closed.

Skip to main content