Connecting Tech Pros Worldwide Forums | Help | Site Map

Running a query on records in a month

Member
 
Join Date: Mar 2007
Posts: 94
#1: Sep 4 '09
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

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Sep 4 '09

re: Running a query on records in a month


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?
Member
 
Join Date: Mar 2007
Posts: 94
#3: Sep 7 '09

re: Running a query on records in a month


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?
Member
 
Join Date: Mar 2007
Posts: 94
#4: Sep 7 '09

re: Running a query on records in a month


Ha! I have had alook and ThisMonths total is also counting last months - any idea why? It's stumping me!
Member
 
Join Date: Mar 2007
Posts: 94
#5: Sep 7 '09

re: Running a query on records in a month


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.
Member
 
Join Date: Mar 2007
Posts: 94
#6: Sep 7 '09

re: Running a query on records in a month


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!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Sep 8 '09

re: Running a query on records in a month


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.
Reply