Skip to main content
Participant
December 1, 2008
Question

cfquery date help

  • December 1, 2008
  • 3 replies
  • 1170 views
Hi,

I am trying to create a cfquery that will search the renewaldate field and will pull up records that will have the same month and year as the current month and year.

Any suggestions on the best way to do this?

TIA
This topic has been closed for replies.

3 replies

Inspiring
December 2, 2008
mSQL has a whole array of built-in date functions you can use:

SELECT ..., your_date_column
FROM members
WHERE YEAR(your_date_column) = YEAR(NOW()) AND MONTH(your_date_column) =
MONTH(NOW())

note that all the functions above are MySQL functions, NOT cf functions,
so DO NOT use # around them.


Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
colohcAuthor
Participant
December 2, 2008
Azadi, That worked great! Thank you

quote:

Originally posted by: Newsgroup User
mSQL has a whole array of built-in date functions you can use:

SELECT ..., your_date_column
FROM members
WHERE YEAR(your_date_column) = YEAR(NOW()) AND MONTH(your_date_column) =
MONTH(NOW())

note that all the functions above are MySQL functions, NOT cf functions,
so DO NOT use # around them.


Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/



Inspiring
December 2, 2008
Your date field might appear like yyyy-mm-dd, but that's a display thing. I don't use mysql but when I googled "mysql date functions" I found this. http://www.w3schools.com/SQL/sql_dates.asp

Year and month functions did not appear, but curdate returns the current date. That being the case, Cold fusion functions are probably a better way to go.

Use a combination of now(), year, month, and dateadd to get the 1st day of the current month and the first day of the next month. Let's call those variables date1 and date2.

They get used in your query like this

where renewal_date >= <cfqueryparam value="#date1#">
and renewal_date < <cfqueryparam value="#date2#">

Inspiring
December 1, 2008
If your db has functions named year and month, and most do, that would be a good start. Equally valuable, and likely available would be a db function that returns the current date.
colohcAuthor
Participant
December 1, 2008
The DB is MySQL so the date field is listed like yyyy-mm-dd

My query is:

<cfquery name="RenewalDate" datasource="#DSN#">
SELECT * FROM Members WHERE RenewalDate LIKE '#DateFormat(Now(),'yyyy-mm')#'
</cfquery>

Not working though as I am not clear on what to put for RenewalDate and the current date.