Date Comparision

  • Table1

    Callstatusflag Generateddate

    N2013-06-24 00:00:00.000

    N2013-06-24 00:00:00.000

    B2013-06-22 00:00:00.000

    C2013-06-22 00:00:00.000

    select count(Call_status_flag) Total from Table1

    where '2013-06-22 00:00:00.000'>='2013-06-22 00:00:00.000' and Call_status_flag='N' O/P:2

    select count(Call_status_flag) Total from Table1

    where Generateddate='2013-06-22 00:00:00.000' and Call_status_flag='N' O/P:0

    Why?

  • You second SQL Statement should produce an error because you don't compare the date to anything. If it is a mistake, pleas rewrite your question and this time write a small script that creates the table and insert the data and then the 2 queries that you used and specify the results that you were expecting. This way you'll get help much faster.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Sorry...

    Plz check now and i posted correctly

  • This is your first query:

    select count(Call_status_flag) Total from Table1

    where '2013-06-22 00:00:00.000'>='2013-06-22 00:00:00.000' and Call_status_flag='N'

    The expected results are 2. The first condition will always be evaluated as true because it compares 2 constants that are the same ('2013-06-22 00:00:00') The second condition looks for any record that has the value 'N' in column call_status_flag. Since there are 2 records that meet these criteria you should get 2 as the queries' results.

    Your second query looks like that:

    select count(Call_status_flag) Total from Table1

    where Generateddate='2013-06-22 00:00:00.000' and Call_status_flag='N'

    Since in this query you check the value of 2 columns and there are no records that have both values in those columns it, you should get 0 as the results.

    One last remark – Pleas read the article that I reference in my signature. This will show you the proper way of asking questions.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply