1

I have a database field like this:

Timestamp varchar(19) utf8mb4_unicode_ci 

containing a timestamp (string) like this

"2013-05-29 00:00:00"

I am using the entity framework and I would like to filter by time - meaning I would like to get all entries having a timestamp > (now-interval). Code would look something like this

var query = from r in db.route
                    where
                        r.timestamp > (now-interval);
                    select r;

How can I do this?

5
  • 2
    Why are your dates stored as varchar in your database? Can you change that? Commented Oct 13, 2015 at 12:17
  • 2
    Possible duplicate of How do I perform Date Comparison in EF query? Commented Oct 13, 2015 at 12:18
  • @gpinkas Not a dup since that deals with a DATE column not a VARCHAR. Commented Oct 13, 2015 at 12:27
  • 2
    You should absolutely change the DB design if possible. Otherwise you're left with retrieving all the records and doing the parsing and filtering in memory, which will be much slower. Commented Oct 13, 2015 at 12:28
  • @juharr Not entirely true as the "date" format is in a usable format for ordering alphanumerically. Commented Oct 13, 2015 at 12:45

1 Answer 1

1

My first suggestion would be to fix the database so the date values are stored as the correct date type. That would solve many issues as well as increase search performance. However, in the (unlikely) situation that you are unable to do that, and that the format of the values in that column all match exactly as you specified in the question, you could convert your local time stamp variable to a string and compare it directly. As the format you have shown has an alphanumeric ordering that is identical to the date order, it should work:

//It's important that this is done here and not inline with the Linq query
var nowInterval = timeStamp.ToString("yyyy-MM-dd HH:mm:ss");

var query = from r in db.route
            where string.Compare(r.timestamp, nowInterval, StringComparison.Ordinal) > 0
            select r;
Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't you use HH for 24 hour clock?
@juharr Oops, well spotted!
Thanks for all the advice! Unfortunately the DB is not at my liberty to change :( When I use your code visual studio reminds me that it cannot apply operator '>' on string and string. It seems that something like: SELECT * FROM yourTable WHERE STR_TO_DATE(my_column_with_the_string_date, '%d.%m.%Y') <= '2011-09-30' could solve the problem, I just don't know how to put it in "Entity FW writing". Any ideas?
@hot33331 Ah yes, I forgot about that. Instead of using > you can use string.Compare, so in this case it will be something like this: string.Compare(r.timestamp, nowInterval, StringComparison.Ordinal) > 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.