Database Query Only Show Current Month Articles

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Database Query Only Show Current Month Articles

    Right now we are having to go in and manually change the date to only show
    this month and years articles, but we'd like to change the query so we can
    only show this month, year using a variable. Here is the query can someone
    tell me what we would need to change it to only show the current months
    articles without manually doing it?


    WHERE type='news' and date > '07/01/2005' and date < '7/31/2005'


    Thank in advance.





    GoBigOrange Guest

  2. Similar Questions and Discussions

    1. Database driven articles
      Hello, I am supernew to ColdFusion and to these forums, and I'm sorry if this is an overly obvious/retarded question. That said: I am...
    2. Query distinct month and date
      ASP 3.0 VB - access We are trying to take a date field and pull the month and year into a drop down menu. I am using access and can format the...
    3. Current month change
      Hello, Total n00b here who needs help. I have a list of weblog (blog) entries in a database which are being retrieved by index.php with a string...
    4. shortcut to show/hide current layer?
      is there a shortcut to show / hide the current layer? thanks felix
    5. Show" Last view... Day - Month - Date - Year on page.
      Hello All; I have just added in a JavaScript function that show that Day - Month - Day - Year On my page. This is going for a look of Last...
  3. #2

    Default Re: Database Query Only Show Current Month Articles

    I'm sorry, but 07/01/2005 just isn't going to fly. What is the format of the
    date field in your database? The current month's articles for this year are
    DateFormat(Now(), "yyyymm"), which is what you could be storing in the
    database. That gives you 200508 for this month.

    jdeline Guest

  4. #3

    Default Re: Database Query Only Show Current Month Articles

    We are already doing a query on the database using that code WHERE type='news'
    and date > '07/01/2005' and date < '7/31/2005'

    I looked at the database in enterprise manager and the data format is 4/2/2005
    9:22:29 AM

    The problem is, we don't need to manually put the dates in, what would the
    code be to pull out the last 30 days

    GoBigOrange Guest

  5. #4

    Default Re: Database Query Only Show Current Month Articles

    Originally posted by: GoBigOrange
    We are already doing a query on the database using that code WHERE type='news'
    and date > '07/01/2005' and date < '7/31/2005'


    I think the point was that the query above only gives you records "dated"
    between 7/2/2005 and 7/30/2005, which is not the entire month of July.




    <!--- find all records "dated" within the current month --->
    <!--- example: 08/01/2005 and 08/31/2005 --->
    <cfset firstOfMonth = CreateDate(Year(Now()), Month(Now()), 1)>
    <cfquery name="getNews" datasource="myAccessDSN">
    SELECT Type, DateColumn
    FROM YourTable
    WHERE type='news' AND
    dateColumn >= #CreateODBCDate(firstOfMonth)# AND
    dateColumn < #CreateODBCDate(DateAdd("m", 1, firstOfMonth))#
    </cfquery>

    <!--- find all records "dated" in the last 30 days --->
    <!--- example: if today is 08/20/2005 .... --->
    <!--- returns records "dated" between 07/22/2005 through 08/20/2005 .... --->
    <cfquery name="getNews" datasource="myAccessDSN">
    SELECT Type, DateColumn
    FROM YourTable
    WHERE type='news' AND
    dateColumn >= #CreateODBCDate(DateAdd("d", -29, Now()))# AND
    dateColumn < #CreateODBCDate(DateAdd("d", 1, Now()))#
    </cfquery>

    mxstu Guest

  6. #5

    Default Re: Database Query Only Show Current Month Articles

    Thanks for the code, it worked like a charm!
    GoBigOrange Guest

  7. #6

    Default Re: Database Query Only Show Current Month Articles

    Okay one more question and I will leave you guys alone! We have a thing that
    shows the next home game on the site. What I would like to do is show a summary
    of the next 5 home games or perhaps all the home games for the current month.
    Here is the query that shows the next home game:

    <cfquery name="gameToday" datasource="#mainDSN#"
    username="#mainDSNUsername#" password="#mainDSNPassword#">
    SELECT *
    FROM schedule
    WHERE date='#dateformat(now(),'mm/dd/yyyy')#' AND (Score IS NULL or Score =
    '') and home = 1 and final != 1
    </cfquery>

    GoBigOrange Guest

  8. #7

    Default Re: Database Query Only Show Current Month Articles

    The next five future games from a specific date.. or the current date? If from
    the current date, should it include a game scheduled for the current date?

    To show all the games for the current month, you can just adapt the first
    query in my example above. It begins with the comment line : <!--- find all
    records "dated" within the current month --->



    mxstu Guest

  9. #8

    Default Re: Database Query Only Show Current Month Articles

    My date format is different on this table, perhaps that is the problem the
    dates are formatted like 10/21/2005. I keep getting a processing error. One of
    the things is the home has to be 1 so I have a where statement too.




    <cfset firstOfMonth = CreateDate(Year(Now()), Month(Now()), 1)>
    <cfquery name="games" datasource="#mainDSN#" username="#mainDSNUsername#"
    password="#mainDSNPassword#">
    SELECT *
    FROM schedule
    WHERE home = 1
    date >= #CreateODBCDate(DateAdd("d", -29, Now()))# AND
    date < #CreateODBCDate(DateAdd("d", 1, Now()))#
    </cfquery>

    GoBigOrange Guest

  10. #9

    Default Re: Database Query Only Show Current Month Articles

    > My date format is different on this table, perhaps that is the problem
    > the dates are formatted like 10/21/2005.
    What database are you using? Is the column a date/time column? What do you
    mean by the format is different than your other table?

    mxstu Guest

  11. #10

    Default Re: Database Query Only Show Current Month Articles

    the column is called date and when we put the schedules in we enter the date of
    the game like 10/21/2005. Probably should have been called something else, but
    someone else did it! 10/21/2005 is a sample of a date in the column.



    GoBigOrange Guest

  12. #11

    Default Re: Database Query Only Show Current Month Articles

    You didn't say whether or not it is a date/time column.

    Also, "Date" is a reserved word, so using it as a column name will likely cause problems.
    mxstu Guest

  13. #12

    Default Re: Database Query Only Show Current Month Articles

    >Also, "Date" is a reserved word, so using it as a column name will likely
    cause problems.

    I take that back. You can use "Date" as a column name in SQL Server database,
    but using another name would be better.

    mxstu Guest

  14. #13

    Default Re: Database Query Only Show Current Month Articles

    that's what I was afraid of. time to rename column.

    GoBigOrange Guest

  15. #14

    Default Re: Database Query Only Show Current Month Articles

    Did you see my question?

    You didn't say whether or not it is a date/time column.
    mxstu Guest

  16. #15

    Default Re: Database Query Only Show Current Month Articles

    no, somebody has named the column date. And they should have called it gamedate or something. The newletter using a real data field that timestamps.
    GoBigOrange Guest

  17. #16

    Default Re: Database Query Only Show Current Month Articles

    I'm talking about the column data type. For example, sql server uses the
    "date/time" data type to store dates, "varchar" to and "char" to text
    characters, etc... Anyway, I think your problem may be that you're missing an
    AND in your statement:

    SELECT *
    FROM schedule
    WHERE home = 1 missing an AND here ....
    date >= #CreateODBCDate(firstOfMonth)# AND
    date < #CreateODBCDate(DateAdd("m", 1, firstOfMonth))#

    mxstu Guest

  18. #17

    Default Re: Database Query Only Show Current Month Articles

    Originally posted by: GoBigOrange
    no, somebody has named the column date. And they should have called it
    gamedate or something. The newletter using a real data field that timestamps.

    The response was not quite clear ;-) If "Schedule.Date" is NOT a "date/time"
    column.... then you are correct... using #CreateODBCDate()# will not work
    because it is intended for use with "date/time" columns. If this is the case,
    you should consider changing the column's data type to "date/time". Dates
    should be stored as dates and not text. Just be aware that the change may
    affect other queries that insert into or update this field.



    mxstu Guest

  19. #18

    Default Re: Database Query Only Show Current Month Articles

    The column data time is datetime ....sorry. :-)
    GoBigOrange Guest

  20. #19

    Default Re: Database Query Only Show Current Month Articles

    There should not be any problem if the data type IS "date/time". Did you
    correct the syntax error I mentioned in the original query?

    SELECT *
    FROM schedule
    WHERE home = 1 AND <--- missing an AND here
    date >= #CreateODBCDate(firstOfMonth)# AND
    date < #CreateODBCDate(DateAdd("m", 1, firstOfMonth))#


    mxstu Guest

  21. #20

    Default Re: Database Query Only Show Current Month Articles

    Yeah, the and sort of helps, huh? That did the trick. Thanks!
    GoBigOrange Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139