count(*) returning incorrect value

  • Hello, I had a strange problem and am wondering if anyone can tell me what could have caused it. Running these queries on a certain table gave the following results:

    select count(*) from Table1

    -> about 19 million

    select count(*) from Table1 where DateColumn < 'Jan 31, 2008'

    -> about 10.5 million

    select count(*) from Table1 where DateColumn >= 'Jan 31, 2008'

    -> about 10.5 million

    (there are actually only 2 different dates in DateColumn, one from the beginning of January and one from the beginning February)

    note: DateColumn is part of the primary key

    After reindexing the table the first query returned the correct result of about 21 million. Does anyone know what the problem could have been and how it could have happened?

    Thanks

  • Hi,

    When you are doing date comparision convert the date to specific format. Can yu post the Table structure with Datatypes.

    Thanks -- Vj

    http://dotnetvj.blogspot.com

  • Hi,topstairs,try like this:

    Create Table table1(

    DateColumn datetime

    )

    Select Count(*) From table1 where convert(varchar(8),DateColumn,112) < '20080226'

    or

    Select Count(*) From table1 where convert(varchar(10),DateColumn,120) < '2008-02-26'

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

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