Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    28

    Query on Month Only of Date Field

    How do I phrase a query to pull data from a table based upon only the month? I tried
    Like "*" & [Enter Month] & "*" but that pulls the wrong months as well as the ones I want.

    I.E. If I want month 1 it'll pull 1 and 10, 11 and 12.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,436
    in short the db is not pulling the wrong months, its doing precisely what you have requested, based on your table/column design

    how are you letting your users define month?
    are you using number (3), abbreviation ("Mar") or full name ("March")
    are you storing the date as sting (yuck) or as a date time value?

    I'd suggest you restrict your users inputs by using a form which validates anythign sent to the SQL engine is legit, that means you can then comntrol what the users does, but it alos means that there is a consistent interface (it means for example that a user can rerun a report or a series of reports using the same date/month)

    so
    if you are storing a date as string (and your really really shouldn't) you can use the like construct
    however it depends on how your dates are stored say they are stored in UK / Aus and other 'sensible countries where the month is the middle part of the date you could look for "*/01/*" to find January or "*/1/*"
    conceivably you may have to do both, you may also have to look for "*Jan/*" AND "*January*"
    if you use US dates
    look for "* 1/" and "* 01/* and so on......
    you will get yourself tied in knots if you persist in storing dates as sting values.

    its far easier if they were stored as date time values

    you'd just use where month(mydatecolumn)= mynumericvalue

    instead of
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2009
    Posts
    28
    The field in the table is a date/time format with the default being date() and the format of m

    How can I get it to just search on the month?

  4. #4
    Join Date
    Feb 2009
    Posts
    28
    Got it!
    I put a column in my query like this:
    MonthValue: Month([DateField])

Posting Permissions

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