Date comparision logic....?

  • Hi Team,

    select create_date, modify_date from sys.objects

    where

    create_date >='2010-04-02 16:59:23.267' and

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    am using above query to display

    Create_date should be >= 2010 and

    modify date shoule be >=Jan 2012 and <=Dec 2012

    But am not getting the exact result.

    Please help..........................:)

  • like this?

    select create_date, modify_date from sys.objects

    where

    create_date >='2010-01-01' and

    (modify_date >='2012-01-01' or modify_date <'2013-01-01')

  • The problem is with this:

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    Your query is looking for record where the modified date = '2012-01-28 15:43:46.077'

    Slight change to Tony's version:

    select create_date, modify_date from sys.objects

    where create_date >='2010-01-01'

    AND modify_date between '2012-01-01' AND '2013-01-01'

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Abu Dina (2/19/2013)


    The problem is with this:

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    Your query is looking for record where the modified date = '2012-01-28 15:43:46.077'

    Nope, its an 11 month window not a 12 month window. 2012-01-28 to the 2012-12-28

  • Abu Dina (2/19/2013)


    The problem is with this:

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    Your query is looking for record where the modified date = '2012-01-28 15:43:46.077'

    Slight change to Tony's version:

    select create_date, modify_date from sys.objects

    where create_date >='2010-01-01'

    AND modify_date between '2012-01-01' AND '2013-01-01'

    Nope again as if anything was modified at 2013-01-01 00:00:00.000 it will also be included which is not what the OP wants

    Hence >= '2012-01-01' and < '2013-01-01'

    Dont get caught out with the issues of between

    Between does a >= <= clause

  • anthony.green (2/19/2013)


    Abu Dina (2/19/2013)


    The problem is with this:

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    Your query is looking for record where the modified date = '2012-01-28 15:43:46.077'

    Slight change to Tony's version:

    select create_date, modify_date from sys.objects

    where create_date >='2010-01-01'

    AND modify_date between '2012-01-01' AND '2013-01-01'

    Nope again as if anything was modified at 2013-01-01 00:00:00.000 it will also be included which is not what the OP wants

    Hence >= '2012-01-01' and < '2013-01-01'

    Dont get caught out with the issues of between

    Between does a >= <= clause

    Oh yea I know the BETWEEN is inclusive, I just misread the user requirements 😛 and the code too lol

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Minnu (2/19/2013)


    Hi Team,

    select create_date, modify_date from sys.objects

    where

    create_date >='2010-04-02 16:59:23.267' and

    (modify_date >='2012-01-28 15:43:46.077' or modify_date <='2012-12-28 15:43:46.077')

    am using above query to display

    Create_date should be >= 2010 and

    modify date shoule be >=Jan 2012 and <=Dec 2012

    But am not getting the exact result.

    Please help..........................:)

    why such exact precision ???

    well I guess writing

    modify_date >='Jan 2012' or modify_date <='Dec 2012'

    would work well too ...

    Edit : using the symbols < and > in the code

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

Viewing 7 posts - 1 through 6 (of 6 total)

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