Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
How to compare the dates in datetime? For instance I am having a field with date as 2008-06-26 16:19:21.890 in db and and I want to fetch the data less than or equal to the selected date from calendar control? I am passing the date as mm/dd/yyyy . On selecting 26/06/2008 date I am not getting data on that date. Earlier dates data are fetched fine. Want to change in Sql itself.

SQL
SELECT  C.CorporateId, C.CorporateName AS CorporateName,E.EmpId, E.FirstName, E.LastName,
    E.IBAN as AccountNo,E.CitizenId,[dbo].[fnMaskCardNo](E.CardNo)as CardNo, E.PassportId,
     E.NationalityCode, Co.CountryName,E.CardStatus,
    C.IsDeleted,E.DOB,E.RHFReceivedDate As Date,
     CONVERT(INT,MONTH(E.RHFReceivedDate)) as ModifiedMonth,
    CONVERT(INT,year(E.RHFReceivedDate)) as ModifiedyEAR
  FROM Employee E,Corporate C, Country Co
  WHERE
    -- CONVERT(VARCHAR(19),E.RHFReceivedDate)<='June  6 2008 12:00AM

    AND E.CorporateId=C.CorporateId
   AND C.RegisterStatusId>=7
   AND E.CardNo IS NOT NULL
  -- AND  E.CardStatus = 'L'
   AND (E.CorporateId='IBM')
   AND C.IsDeleted='0'
   and E.EmpId='46565675'
   AND E.NationalityCode = Co.CountryCode
  ORDER BY  C.CorporateId, E.EmpId
Posted
Comments
King Fisher 29-May-14 3:49am    
any error?

Hi,
less than or equal to the selected date from calendar control...

SQL
DateAdd(dd,Datediff(dd,0,E.RHFReceivedDate),0) <= selected date from calendar control


Please try this, and update
 
Share this answer
 
Because you are storing a time component as well as a date component, when you do your comparison you need to do

DateTime < (SelectedDate + 1 day)

This will all you to return all of the entries for the selected date regardless of their time component.

SQL
E.RHFReceiveDate < '2008-06-29'
 
Share this answer
 
Before You do any filter using Date just Convert it for our safety

Say


Convert(date, E.RHFReceiveDate) < Convert(date, '12/12/2012')

let me knw if this works
 
Share this answer
 
v2
Hi,

Check this...


SQL
BETWEEN CONVERT(DATETIME, E.RHFReceivedDate ,103) <= CONVERT(DATETIME,@your_input_data_para,103) 


Hope this will help you.

Cheers
 
Share this answer
 
use...


SQL
cast(RHFReceivedDate as date)<=cast('26/06/2008' as date)
 
Share this answer
 
v2
try using the Date property on the DateTime Object.
..
if(dtOne.Date == dtTwo.Date)

...

Or Else For a true comparison
..
dateTime1.Date.CompareTo(dateTime2.Date);

..
Or Last Might That I have coded
..
DateTime dt1 = DateTime.Now.Date;
        DateTime dt2 = Convert.ToDateTime(TextBox4.Text.Trim()).Date;
        if (dt1 >= dt2)
        {
            MessageBox.Show("Valid Date");
        }
        else
        {
            MessageBox.Show("Invalid Date... Please Give Correct Date....");

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900