multiplying datediff (time) by other value in query

G

Guest

I have the expression
DateDiff("n",[Start time],[End time])
working to display the number of elapsed minutes (or divided by 60 to get
hours), which I now want to multiply by a cost [single rate] but
DateDiff("n",[Start time],[End time])*[single rate]
doesn't display anything. The cost format in the table is numeric (tho' I
want it to display as currency).
 
J

Jeff Boyce

If any of the three of those fields contains a "Null", be aware that "nulls
propagate".

Can you confirm that NONE of the three, in ANY record contain nulls?

Where are you doing this? In a query? Is there nothing, or is there an
error message (and if so, what does it say)?

--
Regards

Jeff Boyce
Microsoft Office/Access MVP


Microsoft IT Academy Program Mentor
http://microsoftitacademy.com/

Microsoft Registered Partner
https://partner.microsoft.com/
 
G

Guest

In a query. No nulls in rates table; rate is arrived at by joining with
other tables. No error message, just blank field display.

Jeff Boyce said:
If any of the three of those fields contains a "Null", be aware that "nulls
propagate".

Can you confirm that NONE of the three, in ANY record contain nulls?

Where are you doing this? In a query? Is there nothing, or is there an
error message (and if so, what does it say)?

--
Regards

Jeff Boyce
Microsoft Office/Access MVP


Microsoft IT Academy Program Mentor
http://microsoftitacademy.com/

Microsoft Registered Partner
https://partner.microsoft.com/

kwizzy said:
I have the expression
DateDiff("n",[Start time],[End time])
working to display the number of elapsed minutes (or divided by 60 to get
hours), which I now want to multiply by a cost [single rate] but
DateDiff("n",[Start time],[End time])*[single rate]
doesn't display anything. The cost format in the table is numeric (tho' I
want it to display as currency).
 
L

Larry Linson

kwizzy said:
I have the expression
DateDiff("n",[Start time],[End time])
working to display the number of elapsed minutes (or divided by 60 to get
hours), which I now want to multiply by a cost [single rate] but
DateDiff("n",[Start time],[End time])*[single rate]
doesn't display anything. The cost format in the table is numeric (tho' I
want it to display as currency).

Works nicely for me... I created a table with a uniqueID field as
AutoNumber, Start Time and End Time fields as date/time, and Single Rate as
a numeric, single precision floating point.

The Query retrieved the unique ID and I copied your calculation from the
newsgroup, pasted it in and it works just fine. The calculated field in the
Query Builder grid is:

Charge: DateDiff("n",[Start time],[End time])*[single rate]

Is "Single Rate" a field in the same Query? Are you certain you have the
Field Names spelled correctly? Are you certain none of the fields contains
a Null value?

Larry Linson
Microsoft Access MVP
 
J

Jamie Collins

a cost [single rate]

The cost format in the table is numeric (tho' I
want it to display as currency).

I created a table with ... Single Rate as
a numeric, single precision floating point.

I think you have interpreted the OP's column name too literally <g>!

I imagined a hotel where there are different charges based on
occupancy e.g. a single room. The OP implies money, therefore a
DECIMAL column would be most appropriate. If the OP chooses the
CURRENCY data type, I'd urge them to use a validation rule to limit
the range to something appropriate to the domain values e.g. no one
wants a hotel bill for 922,337,203,685,477.5807.

Data that is genuinely floating point in nature is very rare and the
'Single' data type is particularly inappropriate for financial data.

Jamie.

--
 
L

Larry Linson

I think you have interpreted the OP's column
name too literally <g>!

I think you are trolling for an argument again.
Data that is genuinely floating point in nature
is very rare and the 'Single' data type is particu-
larly inappropriate for financial data.

Your statement is correct for real-world production applications. In this
case, however, any type of numeric data is appropriate because it serves to
answer the question. If you wish to create the production application for
the original poster, please take it off-line.

Larry Linson
Microsoft Access MVP
 
J

James A. Fortune

Jamie said:
a cost [single rate]

The cost format in the table is numeric (tho' I
want it to display as currency).

I created a table with ... Single Rate as
a numeric, single precision floating point.


I think you have interpreted the OP's column name too literally <g>!

I imagined a hotel where there are different charges based on
occupancy e.g. a single room. The OP implies money, therefore a
DECIMAL column would be most appropriate. If the OP chooses the
CURRENCY data type, I'd urge them to use a validation rule to limit
the range to something appropriate to the domain values e.g. no one
wants a hotel bill for 922,337,203,685,477.5807.

Check please :).
Data that is genuinely floating point in nature is very rare and the
'Single' data type is particularly inappropriate for financial data.

My philosophy regarding Currency fields in Access is that they are great
for simple financial calulations, but nearly any meaningful calculation
puts the expression into the realm of floating point numbers. Most of
the time I end up using the Double type for calculations and a Currency
value to store the final results.

C.f.:

http://groups.google.com/group/comp.databases.ms-access/msg/7568a8ea7d582abb

where exponentiation is involved in the solution. Even less exotic
operations quickly make Currency formats almost useless.

James A. Fortune
(e-mail address removed)
 
J

Jamie Collins

My philosophy regarding Currency fields in Access is that they are great
for simple financial calulations, but nearly any meaningful calculation
puts the expression into the realm of floating point numbers. Most of
the time I end up using the Double type for calculations and a Currency
value to store the final results.

C.f.:

http://groups.google.com/group/comp.databases.ms-access/msg/7568a8ea7...

where exponentiation is involved in the solution.

Yes, you have to be careful Jet does not coerce values to DOUBLE
FLOATING POINT (SQL keyword in uppercase).

The one most commonly seen is dividing by an INTEGER e.g. (the
following examples use Access/Jet SQL while in ANSI-92 Query Mode):

SELECT TYPENAME(CCUR(123.45) / 100 -- returns 'Double'

Dividing by the equivalent a CURRENCY value yield the same:

SELECT TYPENAME(CCUR(123.45) / CCUR(100)) -- returns 'Double'

Multiplication is better:

SELECT TYPENAME(CCUR(123.45) * CCUR(0.01) -- returns 'Currency'

The case for DECIMAL is enhanced when you consider arithmetic on
values using a native decimal (lowercase) results in a DECIMAL e.g.

SELECT 54321 * 0.01 -- returns 'Decimal'

The key to this is knowing the native decimal vales are of type
DECIMAL e.g.

SELECT TYPENAME(123.45), TYPENAME(1.2345E2) -- returns 'Decimal' and
'Decimal'.

hence dividing a DECIMAL by an INTEGER results in a DECIMAL:

SELECT TYPENAME(123.45 / 100) -- returns 'Decimal'.

I think I am correct in saying the DECIMAL type is the

This is by design and is just as well because the CDEC() casting
function is broken in SQL (http://support.microsoft.com/kb/225931) so
occasionally you have to get 'creative' at the cost of clarity e.g.

contrast:
SELECT 54321 / 12 AS result_value, TYPENAME(54321 / 12) AS
result_type

with:
SELECT 54321 / 0.12 * 0.01 AS result_value, TYPENAME(54321 / 0.12 *
0.01) AS result_type

or

SELECT 54321 * 8.33333333333333333333333333E-02, TYPENAME(54321 *
8.33333333333333333333333333E-02)

Beyond basic arithmetic, floating point results seem to be almost
inevitable :( In SQL, raising a value to the power of another value
coerces the value to DOUBLE regardless of type of those values. Many
Jet, VBA, Access and UDF functions take the 'lowest common
denominator' approach of using Double, often because their
implementation predates VBA6 (which introduced the Decimal type) but
Decimal remains unpopular in VBA (often for no good reason) because it
is not an intrinsic/value type.
Even less exotic
operations quickly make Currency formats almost useless.

Careful of that slippery slope <g>. Jet is strongly-type by design and
perhaps the best approach is to deviate from the type in the reality
being modelled only when necessary.

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top