Select Records only for cetain Year and Month

Discussion in 'Databases' started by adfave, Mar 1, 2010.

  1. #1
    Date column stores date in Y-m-d format i'e 2010-02-25
    Is it possible to select records using only a part of the date(year and month)
    Select * FROM table where date ='2010-02' -- exclude date and get all records for Feb,2010
    I dont want to use between and < > operaters
     
    adfave, Mar 1, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can do something like this.

    Select * FROM table where MONTH(date) ='02' AND YEAR(date) = '2010';

    Just fyi, this will not allow you to use an index on the date column in case there is one. Also, assuming that this is MySQL. For MSSQL you would need another function.
     
    jestep, Mar 1, 2010 IP
    adfave likes this.
  3. redlightshooter

    redlightshooter Greenhorn

    Messages:
    94
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #3
    parsed the date to string use to_char function

    Select * FROM table where to_char(date,'yyyymm') ='201002'
    Code (markup):
     
    redlightshooter, Mar 1, 2010 IP
  4. adfave

    adfave Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Thanks jestep. It's working fine. Green for you both.
     
    adfave, Mar 3, 2010 IP