Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Posts
    51

    Unanswered: searching date column

    i have a table date column with smalldatetime format. data looks like
    2004-05-10 00:00:00

    i can search specify date but i want to retrieve all records for a month for e.g
    April
    how can i do this?
    i tried

    select * from mytable
    where datecolumn like '%05%'
    doesn't seem to work

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    select * from mytable
    where datepart(mm,datecolumn)=5
    and datepart(yyyy,datecolumn)=2004

  3. #3
    Join Date
    Jan 2004
    Posts
    51
    thanks you!!

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    You'll get a lot better performance (especially if dateColumn is indexed) from:
    Code:
    DECLARE @target DATETIME
    SET @target = '2004-04-15'  -- pick any date in the target month
    
    SELECT *
       FROM myTable
       WHERE  Convert(CHAR(8), @target, 121) + '01' <= dateColumn
          AND dateColumn < DateAdd(month, 1, Convert(CHAR(8), @target, 121) + '01')
    -PatP

Posting Permissions

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