Between SYSDATE's with Offset? [message #159960] |
Wed, 22 February 2006 06:42  |
Penfold
Messages: 112 Registered: June 2005
|
Senior Member |
|
|
Hi,
I want to select data only from the previous day, the records has a date field 'TIMESTAMP'. I thought of using 'BETWEEN...AND...', however the previous day starts at 04:00 and runs untill 03:59:59 the following morning.
Using: -
TO_DATE(SA.TIMESTAMP, 'DD.MM.YYYY') BETWEEN TO_DATE(TRUNC(SYSDATE - 1), 'DD.MM.YYYY') AND TO_DATE(TRUNC(SYSDATE), 'DD.MM.YYYY')
The above I understand is using Midnight, am I right?
How can I select only the records where they fall between yesterday 04:00 and this morning 03:59:59?
Any suggestions or advice would be much appreciated.
Regards
|
|
|
|
Re: Between SYSDATE's with Offset? [message #159968 is a reply to message #159965] |
Wed, 22 February 2006 08:14   |
Penfold
Messages: 112 Registered: June 2005
|
Senior Member |
|
|
Thanks for the reply.
In your example 'SYSDATE+5/24' resluted in the correct time, but I noticed it had changed the date to the 23rd?
Also how will this help me with my 'BETWEEN 04:00 AND 03:59'?
Regards
|
|
|
|
Re: Between SYSDATE's with Offset? [message #159976 is a reply to message #159970] |
Wed, 22 February 2006 08:46   |
Penfold
Messages: 112 Registered: June 2005
|
Senior Member |
|
|
Aaah I see...
SQL> select to_char(trunc(sysdate)+4/24, 'dd.mm.yyyy hh24:mi:ss') from dual;
TO_CHAR(TRUNC(SYSDATE)+4/24,'D
---------------------------------------------------------------------------
22.02.2006 04:00:00
What about geting '22.02.2006 03:59:59' though?
Regards
|
|
|
|
Re: Between SYSDATE's with Offset? [message #159979 is a reply to message #159976] |
Wed, 22 February 2006 08:56  |
Art Metzer
Messages: 2480 Registered: December 2002
|
Senior Member |
|
|
To be safe, I would just use greater than or equal to for your start window, and a simple less than for your end window, instead of a BETWEEN.SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> COLUMN ts FORMAT A30
SQL> COLUMN "MUST_BE_GREATER_THAN_OR_=_TO" FORMAT A30
SQL> CREATE TABLE t (id NUMBER, ts TIMESTAMP)
2 /
Table created.
SQL> INSERT INTO t VALUES (1,TO_TIMESTAMP('20060221035959999998','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (2,TO_TIMESTAMP('20060221035959999999','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (3,TO_TIMESTAMP('20060221040000000000','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (4,TO_TIMESTAMP('20060221040000000001','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (5,TO_TIMESTAMP('20060222035959999998','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (6,TO_TIMESTAMP('20060222035959999999','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (7,TO_TIMESTAMP('20060222040000000000','YYYYMMDDHH24MISSFF6'));
SQL> INSERT INTO t VALUES (8,TO_TIMESTAMP('20060222040000000001','YYYYMMDDHH24MISSFF6'));
SQL> SELECT id, ts FROM t
2 /
ID TS
---------- ------------------------------
1 21-FEB-06 03.59.59.999998 AM
2 21-FEB-06 03.59.59.999999 AM
3 21-FEB-06 04.00.00.000000 AM
4 21-FEB-06 04.00.00.000001 AM
5 22-FEB-06 03.59.59.999998 AM
6 22-FEB-06 03.59.59.999999 AM
7 22-FEB-06 04.00.00.000000 AM
8 22-FEB-06 04.00.00.000001 AM
8 rows selected.
SQL> SELECT id
2 , ts
3 , TRUNC(SYSDATE) - 1 + 4/24 "MUST_BE_GREATER_THAN_OR_=_TO"
4 , TRUNC(SYSDATE) + 4/24 must_be_less_than
5 FROM t
6 WHERE t.ts >= TRUNC(SYSDATE) - 1 + 4/24
7 AND t.ts < TRUNC(SYSDATE) + 4/24
8 /
ID TS MUST_BE_GREATER_THAN_OR_=_TO MUST_BE_LESS_THAN
---------- ------------------------------ ------------------------------ --------------------
3 21-FEB-06 04.00.00.000000 AM 21-FEB-2006 04:00:00 22-FEB-2006 04:00:00
4 21-FEB-06 04.00.00.000001 AM 21-FEB-2006 04:00:00 22-FEB-2006 04:00:00
5 22-FEB-06 03.59.59.999998 AM 21-FEB-2006 04:00:00 22-FEB-2006 04:00:00
6 22-FEB-06 03.59.59.999999 AM 21-FEB-2006 04:00:00 22-FEB-2006 04:00:00
SQL>
|
|
|