Results 1 to 6 of 6

Thread: Comparing Dates

  1. #1
    Join Date
    Dec 2004
    Posts
    74

    Unanswered: Comparing Dates

    Does anyone know of a quick and easy to compare 2 datetime values based only on the month and year.

    For example,

    FromDate = 11/30/2004
    ToDate = 12/30/2004

    I just need to compare the 11/2004 to 12/2004 using function like <=, =, >=, etc.

    Any suggestions?

    Thanks

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use:
    Code:
    IF Convert(CHAR(7), FromDate, 121) = Convert(CHAR(7), ToDate, 121)
    -PatP

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    ...or:

    CONVERT(CHAR(7), [YourDate], 120) + '/01'

    ...will implicitly convert your date value to the first day of the month.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by blindman
    ...or:

    CONVERT(CHAR(7), [YourDate], 120) + '/01'

    ...will implicitly convert your date value to the first day of the month.
    My machine doesn't like the mixed separators... It does Ok with:
    Code:
    CONVERT(CHAR(7), [YourDate], 120) + '-01'
    -PatP

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    My bad. I meant "-01"...
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  6. #6
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    I'd create a computed column. This way you can index it and avoid table/clustered index scan overhead.
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •