Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
| Search | Today's Posts | Mark Forums Read
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 4th, 2004, 01:55 AM
Registered User
 
Join Date: Feb 2004
Location: KL, , Malaysia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mcmouli
Default How to subtract hours from sysdate in oracle

I want a query.
I have a date column in one table. Now I want compare this date with (sysdate - 50 hours). How to write query.
How to subtract required hours from the sysdate
Thank you
Mouli

  #2 (permalink)  
Old February 4th, 2004, 12:51 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mcmouli,

You need to compare the date with (sysdate - 50/24) i.e. you need to convert 50 hours into days first. The following execution sequence shows how you can use "decode(sign())" function to compare the dates in the dummy table t.

----------------------------------------------------------------------
SQL> select to_char(dt, 'fmDay, dd Month yyyy hh24:mi:ss') "Date" from t;

Date
-------------------------------------
Thursday, 1 January 2004 15:15:30
Tuesday, 3 February 2004 10:34:45

SQL> select
  2 to_char(dt, 'fmDay, dd Month yyyy hh24:mi:ss') "Date" ,
  3 to_char(sysdate, 'fmDay, dd Month yyyy hh24:mi:ss') "Sysdate - 50 hours",
  4 decode( sign(dt - (sysdate - 50/24)),
  5 -1, 'Date is less than sysdate - 50 hours',
  6 1, 'Date is greater than sysdate - 50 hours',
  7 'Date is equal to sysdate - 50 hours') "Message"
  8 from t
  9 /

Date Sysdate - 50 hours Message
------------------------------------- ------------------------------------- ---------------------------------------
Thursday, 1 January 2004 15:15:30 Wednesday, 4 February 2004 10:37:39 Date is less than sysdate - 50 hours
Tuesday, 3 February 2004 10:34:45 Wednesday, 4 February 2004 10:37:39 Date is greater than sysdate - 50 hours

SQL>

----------------------------------------------------------------------

Hope that helps.

Cheers,
azizmasih


  #3 (permalink)  
Old February 4th, 2004, 12:53 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mcmouli,

Sorry, I goofed up in the query...


SQL> select
  2 to_char(dt, 'fmDay, dd Month yyyy hh24:mi:ss') "Date" ,
  3 to_char(sysdate, 'fmDay, dd Month yyyy hh24:mi:ss') "Sysdate",
  4 to_char(sysdate - 50/24, 'fmDay, dd Month yyyy hh24:mi:ss') "Sysdate - 50 hours",
  5 decode( sign(dt - (sysdate - 50/24)),
  6 -1, 'Date is less than sysdate - 50 hours',
  7 1, 'Date is greater than sysdate - 50 hours',
  8 'Date is equal to sysdate - 50 hours') "Message"
  9 from t
 10 /

Date Sysdate Sysdate - 50 hours Message
------------------------------------- ------------------------------------- ------------------------------------- ---------------------------------------
Thursday, 1 January 2004 15:15:30 Wednesday, 4 February 2004 10:53:32 Monday, 2 February 2004 8:53:32 Date is less than sysdate - 50 hours
Tuesday, 3 February 2004 10:34:45 Wednesday, 4 February 2004 10:53:32 Monday, 2 February 2004 8:53:32 Date is greater than sysdate - 50 hours

SQL>

Cheers,
azizmasih

  #4 (permalink)  
Old January 6th, 2011, 09:41 AM
Registered User
 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to get no of days

Hi,

Could you help me how to get no of days with current date.

I have a table with one column with dateWithTime.

essume that, my table name is xyz, coulumn name is date1.

I need to substract date1 with sysdate like this (sysdate-date1)


Please help me how to write the query.
  #5 (permalink)  
Old January 7th, 2011, 06:52 AM
moonkhan253
Guest
 
Posts: n/a
Default How to subtract hours from sysdate in oracle

There are no SQL functions that works on the current row and the previous or next row. The functions work across all rows.

What you'll need to do is write a PL/SQL procedure or function or anonymous block, where your select statement is a cursor, and process the rows of the cursor, using extra variables to hold the previous row's data to use for comparing against the current row.
____________________
fun | mobile | free download | world call


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtract date stealthdevil Access VBA 2 December 7th, 2007 03:58 PM
Automatically Subtract the Value deepcover1 Access 4 March 14th, 2006 08:33 AM
sysdate mateenmohd SQL Server 2000 3 September 28th, 2003 07:42 AM
Urgent help! Compare a string to Sysdate in Oracle WroteForLuck Classic ASP Basics 1 July 22nd, 2003 01:48 AM
Urgent help needed! Compare a string to Sysdate in WroteForLuck Classic ASP Professional 1 July 17th, 2003 06:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.