Date comparision using Linq - asp.net

I have a DateTime type column named "CreatedDate" in my sql table, and am passing the value for this column by using "DateTime.Now" from my asp.net application....
The datas in my CreatedDate column are,
CreatedDate
-----------
2012-05-07 18:56:17.487
2012-05-07 18:56:28.443
2012-05-07 19:21:24.497
2012-05-14 15:22:04.587
I need to get the datas with this CreatedDate.
in my entity framework I tried the condition like
DataAccess.Entities dataEntities = new DataAccess.Entities();
DataAccess.Employee employee = dataEntities.Employees
.First(e => e.CreatedDate == DateTime.Today);
like this, I have data for this date(2012-05-14) , but the mininutes part differes (the DateTime.Today gives '2012-05-14 12:00:000' like this) here, and it shows error like, sequence contains no element....
How can I compare the 'Date' alone in Linq.....can anyone help me here,,,

Use the Date Property on the DateTime object
CreatedDate.Date==DateTime.Today
So your code will be
DataAccess.Employee employee=dataEntities.
Employees.First(e=>e.CreatedDate.Date==DateTime.Today);
Date Property returns the Date Component of the DateTime object and the time value set to 12:00:00 midnight (00:00:00).

Try this:
DataAccess.Employee employee =
dataEntities.Employees.First(e=>e.CreatedDate.Date==DateTime.Today)

I just declared two variable like
DateTime date1=DateTime.Now.Date;
DateTime date2=DateTime.Now.Date.AddDays(1);
and in the condition I used these variables like
DataAccess.Employee employee = dataEntities.Employees
.First(e => e.CreatedDate >= date1
&& e.CreatedDate < date2);
its working....

Related

SQL Server Filtering by DateTime column, when TIME portion is provided sometimes

In an SSRS report, the user searches based on start date and end date.
The challenge is, as I discovered recently, he sometimes, not always, provides the time component while searching.
Currently, the filter is done like this:
if #pEndDate is null
SET #pEndDate = getdate()
SET #PEndDate = DateAdd(dd,1,#PEndDate)
SELECT ........
FROM .....
WHERE ( Createdon >= #PStartDate AND Createdon < #PEndDate)
This is fine when he searches without time (example - #PStartDate = 2/23/2015 and #PEndDate = 2/24/2015)
How should I structure the query to deal with the time portion when he provides it? (example - #PStartDate = 2/23/2015 15:00 and #PEndDate = 2/24/2015 15:00)
If this is answered elsewhere, please point me to it. Thank you.
If you just want to match the date part then there are lot options.
1) You can use the Date type for the parameter PEndDate and PStartDate to nullify the time part
2) You can use the Convert method to get only date part of the parameter while matching.CONVERT (DATE, #PEndDate) OR CONVERT(varchar,#PEndDate,103)
3) Get Date Part only from DateTime using DateTime functions
ATEADD(dd, 0,
DATEDIFF(dd, 0, #PEndDate))
4) Get Date Part only from DateTime using FLOOR and CAST functions
CAST( -- Convert the integer to DATE
FLOOR(-- Get largest Integer less than or equal to the decimal value
CAST(GETDATE() AS DECIMAL(12, 5)) -- Convert DATETIME to DECIMAL)
AS DATETIME) 'Date Part Only'
5) Get Date Part only from DateTime using DATEPART and CONVERT functions
CONVERT(VARCHAR(4),DATEPART(YEAR, #GETDATE))
+ '/'+ CONVERT(VARCHAR(2),DATEPART(MONTH, #GETDATE))
+ '/' + CONVERT(VARCHAR(2),DATEPART(DAY, #GETDATE))
'Date Part Only'
Use whichever method suits you and you find fancy.
UPDATE
As you mentioned you need to get the time part to 00:00 with date so you can try as,
SELECT CAST( convert(varchar(10),GETDATE(),112) AS DATETIME)
--This will give you 2015-02-27 00:00:00.000
SELECT DATEADD(ms,-3, DATEADD(day, DATEDIFF(day,0,GETDATE())+1,0))
--This will give you end of days time 2015-02-27 23:59:59.997
SELECT CONVERT(nvarchar,getdate(),103) + ' 12:59:59 PM'
--This will give you custom time 27/02/2015 12:59:59 PM

Compare date part of datetime column with NamedQuery

I have a table containing entries with date and time. I try to create a NamedQuery which only compares the date part.
#NamedQuery(name = "Vote.findForDate", query = "SELECT v FROM Vote v WHERE v.createdAt = :date")
...
createNamedQuery("Vote.findForDate").setParameter("date", date, TemporalType.DATE).getResultList();
But it seems that it always tries to compare the whole datetime.
Is there no way without using date() function in SQL?
I try to be independent from the database. For example h2 has no date() function.
One possible solution is the usage of date as column type and reduce the information.
#Column(name = "voteDate")
#Temporal(value = TemporalType.DATE)
private Date voteDate;

How can I remove the Time from a DateTime value?

In my SQL database, I have a column formatted as DateTime and when I retrieve data from that column in ASP.NET, I catch it on the Date variable, than pass the value to textbox:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.ToString
but when I debug my website, the txtSchedDate.Text still gives me the full DateTime value:
7/17/2013 12:00:00 AM
is it possible to eliminate the time value here and just return the date?
Have you tried using something like
txtSchedDate.Text = Y.Date.ToString("MM/dd/yyyy")
or which ever format you wish to display.
Have a look at
DateTime.ToString Method (String)
Converts the value of the current DateTime object to its equivalent
string representation using the specified format.
Custom Date and Time Format Strings
Standard Date and Time Format Strings
Convert.ToDateTime(dt.Rows(0)("SCH_DATE")).ToString("M/d/yyy")
you can get date by txtSchedDate.Text = Y.Date.ToShortDateString()
Besides answers above, you can try converting it in SQL server
SELECT CONVERT(varchar(15), GETDATE(), 11)
Keep in mind after converting it's VARCHAR(15) instead of DATETIME.
Once you have a Date object, you can get the constituent pieces if you wish as well, like this:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.Year & "-" & Y.Date.Month & "-" & Y.Date.Day
Or you can use the custom and standard date and time format strings mentioned by others.

How to get only the date excluding time in asp.net c#

How to get only the date excluding time in asp.net c#. I want only the date to be given as input to search like eg 3/11/2013
You can use DateTime.Date to get only date part of DateTime object
DateTime dateOnly = date1.Date;
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
If you have the Date in string and want to convert it to DateTime object first then you can use DateTime.ParseExact
result = DateTime.ParseExact("3/11/2013", "d/MM/yyyy", CultureInfo.InvariantCulture);
try to use this:
#{
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/yyyy");
}
Now to view just:
#date
DateTime dt = your_Dt.Date;
OR
You can format it into whatever format you want as below:
dt.Tostring("MM/dd/yyyy");
OR
You can convert the valued to shortdate as:
Convert.ToDateTime(your_Dt).ToShortDateString();
I saved the database as datetime in MS-SQL 2014, but when i need to only in date i do as below .... in cshtml
#foreach (var item in Model.dbModelLst)
{
<tr>
<td>#item.ChargeFrequency</td>
<td>#item.Charge</td>
<td>#item.FromDate.ToShortDateString().ToString()</td>
<td>#item.ToDate.ToShortDateString().ToString()</td>
<td>#item.Inv_SerialNo.SerialNo</td>
<td>#item.IncludeLic</td>
<td>#item.Status</td>
.....
.....
</tr>
}
Where dbModelist contain List of Model (IEnumerable)... I Solve this way. Thank you.

Computed time column in SQL Server

Using vs2008 I have a SQL Server database attached to my web app. I want to use a computed time column in the database, along the lines of :
timenow is 1 column
hoursleft is another column
timeend would be another column.
I want timeend to = timenow + hoursleft.
Is it possible to do that, and if so what would the formula be that I would enter into the computed column field, and, what datatype would the columns be, timenow and timeend I would expect to be time(7) and hoursleft an int. But is that correct? THanks for any help.
TimeNow is datatype datetime, populated with function GetUtcDate().
HoursLeft is an int, populated as required.
TimeEnd is a computed column of type datetime, computed as DateAdd(hh, HoursLeft, TimeNow).
You'd use this to calculate timeend from the 2 existing columns hoursleft and timenow
CAST(DATEADD(hour, hoursleft, timenow) AS time(7))
DATEADD return type is
The return data type is the data type of the date argument
So the explicit CAST probably isn't needed if timenow is time(7) as you mentioned
DATEADD(hour, hoursleft, timenow)

Resources