Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Date and time calculations
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. Subtracting a number from
> another <br>number is okay. 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"> '3'-'2'</font></tt> <br><tt><font
> color="#FF0000">----------</font></tt> <br><tt><font
> color="#FF0000">
> 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"> *</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. 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. 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>
> <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> id
> <br> timestart
> <br> timestop
> <br> timetotal <p>The value of
> id is 1 and will be the only row in the table. I can then
> <br>update the table for the start time, and then several hours later
> update <br>the table with the stop time. I get the values from
> sysdate. 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. I get this error: <p>select
> to_char(timestop, 'HH24:MI:SS') - to_char(timestart, 'HH24:MI:SS') AS
> total
> <br> * <br>ERROR at line 1:
> <br>ORA-01722: invalid number
> <p>I'm this is an extreme beginners mistake. Can anybody tell me
> what I
> <br>need to do to get this right?
> <p>Thanks,
> <br>Michael</blockquote>
>
> <br> </html>
Received on Tue Aug 06 2002 - 09:44:57 CDT
![]() |
![]() |