ColdFusion: how to check 15 days past the submitted date? - coldfusion

I saved several accounts in the database. I want to email the accountsdd that have been expired for 15 days. In another word, if the submitted day (date format: mm-dd-yyyy) is 15 days older from today's date then send an email. How do I do it? Any info is greatly appreciated. Thank you.

you would want to use the dateDiff function
<cfif dateDiff('d',submittedDate,now()) GT 15>
If your date is really stored as mm-dd-yyyy instead of a date/time object then you would need to use the createODBCDate function
<cfif dateDiff('d',CreateODBCDate(submittedDate),now()) GT 15>
If you're looking to pull all accounts via a query this would work. This would work on MSSQL
SELECT relevant, columns
FROM myTable
WHERE dateDiff(d,submittedDate,getDate()) > 15

Without more details (database type, data types, etectera) this is just an educated guess. But it sounds like you are asking how to find all records that were submitted exactly fifteen days ago, regardless of the time. For example if the date and time now is July 29, 2013 08:49 AM, you want to retrieve all records submitted on July 14, 2013 (any time between 12 midnight and 11:59:59 PM).
A query with a simple range comparison should do the trick. Note: While Matt's suggestion of dateDiff (or your database's version of it) would also work, using functions in that manner often prevent the database from leveraging indexes. So it is preferable to use a more index friendly expression, like:
<cfset fifteenDaysAgo = dateAdd("d", -15, now())>
<cfquery ...>
SELECT emailAddress
FROM yourTable
WHERE submittedDate >= <cfqueryparam value="#fifteenDaysAgo#" cfsqltype="cf_sql_date">
AND submittedDate < <cfqueryparam value="#dateAdd('d', 1, fifteenDaysAgo)#" cfsqltype="cf_sql_date">
</cfquery>
Then it is just a matter of using the query results to send out your emails.

Related

Unexpected error using the sum function in a QoQ

While attempting to run the following code
<cfquery name="Lev1CatTotal" dbtype="query">
SELECT
SUM(AMOUNT) AS TOTAL
FROM
ChartData
</cfquery>
This is the error message that's generated:
Query Of Queries runtime error. The aggregate function [SUM(expression)] cannot operate on an operand of type [JAVA_OBJECT]
This code works fine when aggregating smaller amounts. However, these are the amount in the table I'm aggregating. This particular query sums to over $5.7B.
AMOUNT
FISCAL_YR
GOV_LEVEL1_CAT
979241575.14
2019
Charges for Services
97218277.18
2019
Charges to Other Governments
233197655.52
2019
Federal Aid
329567996.81
2019
Other Local Revenues
86957092.75
2019
Other Non-Property Taxes
158997846.75
2019
Other Real Property Tax Items
371012673.89
2019
Other Sources
346575244.01
2019
Proceeds of Debt
1145011131.99
2019
Real Property Taxes and Assessments
945308275.55
2019
Sales and Use Tax
921087680.04
2019
State Aid
107357596.20
2019
Use and Sale of Property
Just to move forward, as a workaround, I recoded this as follows:
<cfset TOTAL = 0>
<cfloop query="ChartData">
<cfset TOTAL = precisionEvaluate(TOTAL + AMOUNT)>
</cfloop>
Using precisionEvaluate(), it casts the TOTAL to BigDecimal precision and avoids the error. Does someone know of a QoQ solution using the sum() function to cast this to a big decimal and avoid using this workaround? Thanks.
Big thanks to #BernhardDöbler for getting me to look into where ChartData comes from. Since this was inherited code, I had to look into this. It turned out that ChartData was created with the following line of code.
<cfset ChartData = QueryNew("FISCAL_YR, GOV_LEVEL1_CAT, AMOUNT")>
I noticed, the original coder didn't specify any data types for his QueryNew() statement, so I modified the line of code to
<cfset ChartData = QueryNew("FISCAL_YR, GOV_LEVEL1_CAT, AMOUNT", "VarChar, VarChar, Double")>
Once I added the Double data type to the AMOUNT column, it corrected the error when I restored back to the original code of
<cfquery name="Lev1CatTotal" dbtype="query">
SELECT
SUM(AMOUNT) AS TOTAL
FROM
ChartData
</cfquery>
and I was able to remove my workaround code which performed the aggregation using a <cfloop>.

Subtracting current date from column to show in oracle apex classic report

I have a very simple question but since i am not familiar with SQL or PL/SQL, i got no idea to do that.
In my Oracle APEX Application, I am loading data from a table into a CLASSIC REPORT through setting Local Database/SQL Query as source.
I have to make 4 columns from data of 2 columns stored in a table. I can load 3 without any issue using the below simple statement:
Select TaskName, DueDate, DueDate - 3 as ReminderDate
from table_name
Fourth column should be "RemainingDays" which equals to DueDate-current date, I have tried writing DueDate - Sys_date and DueDate - current_date in the above statement to get the fourth column but probably its not the correct way as i get error instead of all 4 columns. (I am doing in it basic excel/dax way). Any Help here?
When you subtract a date from another date, Oracle returns a number which is the number of days between the two dates.
One thing to note when using SYSDATE or CURRENT_DATE is that you may get different results if your user is not in the same timezone as the database. SYSDATE returns the current time of the database. CURRENT_DATE returns the current time of the user whatever timezone they may be in.
If possible, try building the query in a tool such as SQL Developer, get it working there, then build your Classic Report in APEX. If you are still receiving an error, please share the error you are receiving as well as the query you are using.
Example
--Start of sample data
WITH
t (task_name, due_date)
AS
(SELECT 'task1', DATE '2020-9-30' FROM DUAL
UNION ALL
SELECT 'task2', DATE '2020-9-28' FROM DUAL)
--End of sample data
SELECT task_name,
due_date,
due_date - 3 AS reminder_date,
ROUND (due_date - SYSDATE,2) AS days_remaining
FROM t;
Result
TASK_NAME DUE_DATE REMINDER_DATE DAYS_REMAINING
____________ ____________ ________________ _________________
task1 30-SEP-20 27-SEP-20 13.66
task2 28-SEP-20 25-SEP-20 11.66

How to count records by grouping them in cfWheels?

I have a case where I need to count the number of records grouped by publishing year. I've looked at the documentation, and the net in general, but I can't find what to use.
e.g.
2013 = 100 books published
2012 = 95 books
etc..
Using Oracle SQL, this is done using:
select date_published, count(*)
from publications
group by date_published
order by date_published desc
I'm just wondering how to translate this to CFWheels.
Try this:
publications=model("publication").findAll(
select="date_published, COUNT(date_published) AS publishCount"
, group="date_published"
, order="date_published DESC" );
NB, COUNT() is a case-sensitive command in wheels.
PS, or you can do what matt says - you could even attach it to the model so you could do publications.getPubCountByYear() etc.
This is more of a comment, but because I need the formatting I'm posting it as an answer. Can't you write a query just like a regular query in ColdFusion?
<cfquery name="getCounts" datasource="myDSN">
select date_published, count(*)
from publications
group by date_published
order by date_published desc
</cfquery>

Coldfusion: Convert euro date to us date

I have a date picker sending string dates in Euro format, e.g. 02/12/2011 (December 2, 2011). However, when I try to prepare the date in US format so the database can deal with it, e.g.
dateformat(LSDateFormat(form.startDate),'yyyy-mm-dd')
or
dateformat(form.startDate,'yyyy-mm-dd')
it convolutes the date to 2011-02-12 (February 12, 2011).
Does anyone have a solution that can handle dates - either as strings or date objects.
Cheers,
Paul
How does this work for you:
<cfscript>
euDateArr = ListToArray(form.startDate, '/');
dateObj = CreateDate(euDateArr[3], euDateArr[2], euDateArr[1]);
</cfscript>

What is the quickest way in ColdFusion to get first and last day of quarter?

What is the quickest way in ColdFusion to get first and last day of quarter?
There doesn't seem to be a built in function for this.
First day of quarter:
FirstDayOfQuarter = CreateDate(year, (quarter-1)*3 + 1, 1)
Last day of quarter:
LastDayOfQuarter = DateAdd("d", -1, DateAdd("m", 3, FirstDayOfQuarter))
I have a feeling that your question might be more complex that it appears... for most purposes these values are a known set - no need to calculate:
Quarters:
January 1 - March 31
April 1 - June 30
July 1 - September 30
October 1 - December 31
Since the set is known in advance there's no real need for a function for this - determine in which date a quarter falls is a simple series of "if" statements (psuedocode):
if date > Oct 1 then Q4 else
if date > Jul 1 then Q3 else
if date > Apr 1 then Q2 else
if date > Jan 1 then Q1
(You do the check backwards in this case to check for the most restrictive match first. Although, as Kimvais points out, there is a function to do exactly that already in CFML.)
It should be essentilly the same for other "quarter" systems unless those dates are calculated in some way.
If I've missed the mark feel free to add comment to clarify.
The problem is that "quarter" is a relative term, while many organizations follow the default quarter breakdown of a year starting Jan 1 through Dec 31, many other organizations follow other quarters.
For example most retail organizations. Particularly those that depend on Christmas, don't want to be spending time doing end of quarter/year financials in december. They also want the entire holiday season (including the 2 weeks afterwards) on the same books as the rest of the season. So for them the "year" begins Feb 1.
The U.S. federal government and most of the states begin their financial year Oct 1 because of the way the legislature, elections and budgets work.
So a single function that always worked off of just one quarter layout would never work. Any of the functions listed in the other answers are fine I'm sure as long as your program is only dealing with one set. But if your coding a general use application then you may want to make it configurable.
Looks like there is a function for finding out the Quarter, based on this you could hardcode these?
I don't think there are built in functions for this - it's not clear whether you're looking for the numeric day of month or the string day of month (e.g. Monday). Anyway, this may be a little over-the-top - two functions which require an integer quarter value, and return the first and last dates of the quarter for further manipulation:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="2009">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="2009">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
<cfset year = "2009">
<cfset startmonth = "1">
<cfloop index="quarter" from="1" to="4">
<cfoutput>
<h2>Quarter #quarter#</h2>
#DateFormat(QuarterFirstDate(quarter, year, startmonth))#, day #DayOfYear(QuarterFirstDate(quarter, year, startmonth))#, #DayOfWeekAsString(DayOfWeek(QuarterFirstDate(quarter, year, startmonth)))#<br />
#DateFormat(QuarterLastDate(quarter, year, startmonth))#, day #DayOfYear(QuarterLastDate(quarter, year, startmonth))#, #DayOfWeekAsString(DayOfWeek(QuarterLastDate(quarter, year, startmonth)))#<br />
</cfoutput>
</cfloop>
edit: updated to allow a quarter start month to be specified
How about?
<cfset dt = now()>
<cfset DateLastQuarter = DateAdd("m",-3,dt)> <!--- Any date three months ago falls in previous quarter --->
<cfset quarterNumber = (month(DateLastQuarter)-1)\3+1> <!--- range from quarter 1-4 --->
<cfset StartQuarterMonth = (quarterNumber-1)*3+1> <!--- start of quarter month-number where Jan=1 (Mathematical magic) --->
<cfset LastQuarterFrom = CreateDate(year(DateLastQuarter),StartQuarterMonth,1)>
<cfset LastQuarterTo = DateAdd("d",-1,DateAdd("m",3,LastQuarterFrom))> <!--- the day before three months later --->
this above returns the first date in the previous quarter in LastQuarterFrom
and the last date in the previous quarter in LastQuarterTo