Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Date and time calculations

Re: Date and time calculations

From: Michael Segulja <msegulja_at_yahoo.com>
Date: Tue, 06 Aug 2002 14:44:57 GMT
Message-ID: <pan.2002.08.06.09.41.04.999341.3744@yahoo.com>


Thanks everybody for your help. I new it was simple.

Thanks again.
Michael

On Wed, 31 Jul 2002 16:32:04 -0500, Thomas Gaines wrote:

> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html>
> Michael -
> <p>When doing math, you need to make certain that the values that
> <br>you're manipulating make sense.&nbsp; Subtracting a number from
> another <br>number is okay.&nbsp; Subtracting a date from another date
> is also okay.
> <br>Subtracting a character string from another character string is
> <br>generally not okay unless those characters can be cast to numbers.
> <p>As an example, try this:
> <br><tt><font color="#FF0000">SQL> select '3' - '2' from
> dual;</font></tt><tt><font color="#FF0000"></font></tt> <p><tt><font
> color="#FF0000">&nbsp;&nbsp; '3'-'2'</font></tt> <br><tt><font
> color="#FF0000">----------</font></tt> <br><tt><font
> color="#FF0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> 1</font></tt><tt><font color="#FF0000"></font></tt> <p>Then try this:
> <br><tt><font color="#FF0000">SQL> select 'tom' - 'david' from
> dual;</font></tt> <br><tt><font color="#FF0000">select 'tom' - 'david'
> from dual</font></tt> <br><tt><font
> color="#FF0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *</font></tt>
> <br><tt><font color="#FF0000">ERROR at line 1:</font></tt> <br><tt><font
> color="#FF0000">ORA-01722: invalid number</font></tt> <p>In your case,
> you'll have two character strings with colons embedded <br>in them, and
> those are the cause of your error.&nbsp; You could do this <br>(note the
> absence of colons):
> <p><tt><font color="#FF0000">select to_char(timestop, 'HH24MISS') -
> to_char(timestart, 'HH24MISS')</font></tt> <br><tt><font
> color="#FF0000">as total from timeclock;</font></tt> <p>but the result
> would be pretty meaningless.&nbsp; Try it yourself if you're unsure.
> <p>If you're looking for time passage, simply subtract the two date
> fields <br>without doing a cast:
> <p><tt><font color="#FF0000">select timestop - timestart as total from
> timeclock;</font></tt>
> <p>The result will be in the number of days, and it shouldn't be too
> difficult <br>to get this difference in other units. <p>For fun, do
> this:
> <p><tt><font color="#FF0000">alter session set nls_date_format =
> 'mm/dd/yyyy hh24:mi:ss';</font></tt>
> <br><tt><font color="#FF0000">select sysdate, sysdate + 10 from
> dual;</font></tt> <p>What do you get? <p>Bye,
> <br>Tom
> <br>&nbsp;
> <p>Michael Segulja wrote:
> <blockquote TYPE=CITE>I'm sure this is a very simple question, but I'm
> having trouble for some
> <br>reason getting the sql correct.
> <p>I have a table called timeclock that very simplay has these fields:
> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timestart
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timestop
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timetotal <p>The value of
> id is 1 and will be the only row in the table.&nbsp; I can then
> <br>update the table for the start time, and then several hours later
> update <br>the table with the stop time.&nbsp; I get the values from
> sysdate.&nbsp; I can then
> <br>view the table with this:
> <p>select to_char(timestop, 'HH24:MI:SS') AS stop, to_char(timestart,
> 'HH24:MI:SS') <br>AS start from timeclock; <p>I want to subtract
> timestop from timestart, and get the total time <br>worked, so I wrote a
> query like this: <p>select to_char(timestop, 'HH24:MI:SS') -
> to_char(timestart, 'HH24:MI:SS') <br>as total from timeclock;
> <p>But it doesn't work.&nbsp; I get this error: <p>select
> to_char(timestop, 'HH24:MI:SS') - to_char(timestart, 'HH24:MI:SS') AS
> total
> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * <br>ERROR at line 1:
> <br>ORA-01722: invalid number
> <p>I'm this is an extreme beginners mistake.&nbsp; Can anybody tell me
> what I
> <br>need to do to get this right?
> <p>Thanks,
> <br>Michael</blockquote>
>
> <br>&nbsp;</html>
Received on Tue Aug 06 2002 - 09:44:57 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US