Skip to main content

Posts

Showing posts with the label datetime

Truncate/Ignore time part of DateTime column in Entity Framework

There are times when you want to perform query on only date part of the column that has type DateTime in database. Entity framework does not support DateTime.Date property but provides a helper class EntityFunctions . EntityFunctions has a method TruncateTime that can be called in Linq2Entities query and truncates the time part from datetime. Here is how to use it using (AccountEntities dal = new AccountEntities ()) {               DateTime dtStart=DateTime.Now.AddDays(-20).Date;               DateTime dtEnd=DateTime.Now.Date;     transactions = (from t in dal.Transactions                             where                             EntityFunctions.TruncateTime (t.TransactionDate.Value)                     ...

Playing with Date and Time in MySQL

Manipulating date and time in database is an important task in any database driven application. MySQL is the most popular open source database deployed widely. Here are some tasks that you can perform on date and time using MySQL built in functions. Adding interval to a date DATE_ADD function allows adding/subtracting(using - sign) any interval to date/datetime. The usage of this function is as follows DATE_ADD(date,INTERVAL  [expression] [unit] ) unit can be one of the following MICROSECOND,SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER,YEAR,SECOND_MICROSECOND,MINUTE_MICROSECOND,MINUTE_SECOND,HOUR_MICROSECOND,HOUR_SECOND,HOUR_MINUTE,DAY_MICROSECOND,DAY_SECOND,DAY_MINUTE,DAY_HOUR,YEAR_MONTH expression  depends upon the unit. We will explore some units and respected expressions in this post. Note: All example queries use column name "column_dt" for a column of type datetime. Adding SECOND(s) to DATETIME Following query will add 1 second to the value of column_dt...