selecting records based on date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eugene Anthony

    selecting records based on date

    I have a table that has a DateTime column which uses a DataTime
    datatype. How do I retrieve a range of records based on the month and
    year using ms sql?

    Eugene Anthony

    *** Sent via Developersdex http://www.developersdex.com ***
  • Ed Murphy

    #2
    Re: selecting records based on date

    Eugene Anthony wrote:
    I have a table that has a DateTime column which uses a DataTime
    datatype. How do I retrieve a range of records based on the month and
    year using ms sql?
    Google is your friend.

    Results 1 - 10 of about 12,100 for
    "microsoft sql server" "date functions". (0.27 seconds)

    Comment

    • Eugene Anthony

      #3
      Re: selecting records based on date

      This works.

      SELECT DATEPART(mm, Dates) AS month,DATEPART( yy, Dates) AS year FROM
      testing

      Eugene Anthony

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Utahduck@hotmail.com

        #4
        Re: selecting records based on date

        On Mar 15, 12:44 pm, Eugene Anthony <solomon_13...@ yahoo.comwrote:
        I have a table that has a DateTime column which uses a DataTime
        datatype. How do I retrieve a range of records based on the month and
        year using ms sql?
        >
        Eugene Anthony
        >
        *** Sent via Developersdexht tp://www.developersd ex.com***
        I've always hated this one. Searching for a date like that is pretty
        simple but looking for a range can often times return unwanted
        results. Though I don't know if it is right or not, I've done the
        following before in the past:

        SELECT *
        FROM table
        WHERE CONVERT(char(2) , DatePart(yy, table.datefield )) +
        CONVERT(char(2) , DatePart(mm, table.datefield )) >= CONVERT(char(2) ,
        DatePart(yy, BeginDate)) + CONVERT(char(2) , DatePart(mm, BeginDate))
        AND CONVERT(char(2) , DatePart(yy, table.datefield )) + CONVERT(char(2) ,
        DatePart(mm, table.datefield )) <= CONVERT(char(2) , DatePart(yy,
        EndDate)) + CONVERT(char(2) , DatePart(mm, EndDate))

        This way, if begin date is 1/1/07 and end date is today it will
        evaluate between 0701 and 0703. The problem with the above is that
        since you can't index it it needs to do a full table scan and if it is
        a large table this can sometimes take some time. If that is the case
        and you run this often you might want to add a computed field that
        carries the converted date over and index that sucker.

        I hope that helps.

        Utah

        Comment

        • Jack Vamvas

          #5
          Re: selecting records based on date

          Something like : SELECT myCol1 FROM myTable WHERE DATEPART(mm, Dates) =
          '<insert month>',DATEPAR T(yy, Dates) = '<insert year>'



          --

          Jack Vamvas
          _______________ _______________ _____
          Advertise your IT vacancies for free at - http://www.ITjobfeed.com



          "Eugene Anthony" <solomon_13000@ yahoo.comwrote in message
          news:45f99426$0 $502$815e3792@n ews.qwest.net.. .
          >I have a table that has a DateTime column which uses a DataTime
          datatype. How do I retrieve a range of records based on the month and
          year using ms sql?
          >
          Eugene Anthony
          >
          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          Working...