473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Running a query on records in a month

139 100+
Hi,

I'm trying to extract records for this month and output the count as a figure. I also want to do this for last month. Obviously this will be dynamic so the months update themselves with the system clock.

I have written the code below but it doesn't seem to pull the correct records from my db - I only have records in there for August (Short Date dd/mm/yyyy) but it shows the same figure for Sept & Aug.

Can anyone help?

Expand|Select|Wrap|Line Numbers
  1. <cftransaction>
  2. <cfset ThisMonth = Month(Now())>
  3. <cfset LastMonth = Month(Now())-1>
  4. <cfset ThisYear = Year(Now())>
  5. <cfset DIM = DaysInMonth(Now())>
  6. <cfset TMDate = '01/#ThisMonth#/#ThisYear#'>
  7. <cfset LMDate = '#DIM#/#LastMonth#/#ThisYear#'>
  8. </cftransaction>
  9.  
  10.  
  11. <!--- Start of Colfusion block. Builds list data --->
  12. <cftransaction>
  13.  
  14.     <cfquery datasource="wflow" name="GetWIRecTM">
  15.     select        *
  16.     from        tblRecords    
  17.     where        DateRecordAdded Between #CreateODBCDate("#TMDate#")# AND #createODBCDate("#LMDate#")#                    
  18.  
  19.  
  20.     </cfquery>
  21.  
  22.  
  23.     <cfquery datasource="wflow" name="GetWIRecLM">
  24.     select        *
  25.     from        tblRecords    
  26.     where        DateRecordAdded BETWEEN #CreateODBCDate("01/#LastMonth#/#ThisYear#")# AND #CreateODBCDate("#DIM#/#LastMonth#/#ThisYear#")#
Thanks
Neil
Sep 4 '09 #1
6 3314
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. DateRecordAdded Between #CreateODBCDate("#TMDate#")# AND #createODBCDate("#LMDate#")#
looks incorrect. It should be between the first and last of this month?
Sep 4 '09 #2
ndeeley
139 100+
Hey! Welcome back!

Yes, that was wrong - I've corrected it and it still isn't working. Do you think it might be because the original DateRecordAdded isn't going in as a CreateODBC date?
Sep 7 '09 #3
ndeeley
139 100+
Ha! I have had alook and ThisMonths total is also counting last months - any idea why? It's stumping me!
Sep 7 '09 #4
ndeeley
139 100+
It's strange. Going back to basic code this query works:

Expand|Select|Wrap|Line Numbers
  1. DateRecordAdded BETWEEN #createODBCDate('01-Sep-09')# AND #createODBCDate('30-Sep-09')#
  2.  
But this doesn't:

DateRecordAdded BETWEEN #createODBCDate('01-#ThisMonth#-09')# AND #createODBCDate('30-#ThisMonth#-09')#

where ThisMonth is

Expand|Select|Wrap|Line Numbers
  1. <cfset ThisMonth = Month(Now())
and evaluates to 9.
Sep 7 '09 #5
ndeeley
139 100+
Hmm.

Managed to get it to work by doing this:

Expand|Select|Wrap|Line Numbers
  1. <cfset ThisMonth = Month(Now())>
  2. <cfset TMS = MonthAsString(ThisMonth)>
  3. <cfset LastMonth = Month(Now())-1>
  4. <cfset LMS = MonthAsString(LastMonth)>
  5. <cfset ThisYear = Year(Now())>
  6. <cfset DIM = DaysInMonth(Now())>
  7.  
  8. <cfquery name="GetThisMonth" datasource="wflow">
  9.  
  10.     select         ID, 
  11.             SchemeType,
  12.             AddedByFK,
  13.             SchemeCode,
  14.             SchemeTitle,
  15.             DateRecordAdded
  16.     from        tblRecords
  17.     where        DateRecordAdded BETWEEN #createODBCDate('01-#TMS#-#ThisYear#')# AND #createODBCDate('#DIM#-#TMS#-#ThisYear#')#
  18.     order by    DateRecordAdded
  19.  
  20. </cfquery>
  21.  
But it feels that I`ve bodged it somehow!
Sep 7 '09 #6
acoder
16,027 Expert Mod 8TB
It'll work with the month as a string because then it's the correct date whereas with numbers, the month will be first. To avoid these problems with numbers, you could use the format yyyy-mm-dd.
Sep 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kissi5559 | last post by:
Hello, Could anyone help me with a query? I have a date field and want to display only records for a specified month. Example: Date:5/12/2004 Query for all records in the month of May, 2004. ...
1
by: ED | last post by:
I currently have an ODBC query that hits an Oracle database. I want to bring back records for a given month based on a job completion date in the Oracle database. I would like to have the user...
13
by: Sue | last post by:
I'm working on a database that keeps track of employees hired by a general contractor on a project by project basis. These employees are hired to work on a project and are then laid off either at...
25
by: artromanov | last post by:
Hello everyone. I am somewhat new to access and this forum. Hopefully it will be a great and helpful experience. My problem is this. I have a database (about 65,000 records). I get a run from...
9
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with...
1
by: MLH | last post by:
I've seen suggestions showing how to display running averages on reports in this NG, but not too much on displaying them with a query. I'm wondering if it's just not possible, or if it's just not...
4
kcdoell
by: kcdoell | last post by:
Hello: What is the best way to stop a report from running if the query is empty? Currently, I have a form that has a command button on it. The user has to make selections from 3 combo boxes on...
8
by: limperger | last post by:
Hello everyone! First and foremost, my apologies for the title of the post. It is not very clarifying of what the problem is about, but I didn't know how to put it... My problem is as follows: I...
3
by: JCM82 | last post by:
I have an SMS database that keeps track of any Safety Issues within the company. The query that I have now allows me to select all records for a certain month and year. What I want to be able to do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.