473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing dates using date function and arithmetic

I have a delete statement that is not doing what I want it to do:

Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21
DAYS);

It is supposed to delete all records that are 21 days or older than the
current system date. Instead it is deleting all new rows. LOG_TS is a
timestamp but that should not matter since DATE returns just the date
portion of a date or timestamp...right? I'm not a SQL guru but I can't
see anything wrong with this. I've looked thru the groups and checked
the IBM website but I can't find anything that addresses something like
this. Help!

Nov 12 '05 #1
6 39656
<br**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have a delete statement that is not doing what I want it to do:

Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21
DAYS);

It is supposed to delete all records that are 21 days or older than the
current system date. Instead it is deleting all new rows. LOG_TS is a
timestamp but that should not matter since DATE returns just the date
portion of a date or timestamp...right? I'm not a SQL guru but I can't
see anything wrong with this. I've looked thru the groups and checked
the IBM website but I can't find anything that addresses something like
this. Help!

What is CURRENT_DATE? Shouldn't you be using CURRENT DATE?

It will run faster if you skip the conversion to DATE on both sides of the
equation.
Nov 12 '05 #2
>What is CURRENT_DATE? Shouldn't you be using CURRENT DATE?

I questioned that as well but found examples using CURRENT_DATE and
CURRENT DATE so I'm a little confused.
It will run faster if you skip the conversion to DATE on both sides of

the
equation.

So I don't need to convert the timestamp value to a date using the DATE
function?

Nov 12 '05 #3
<br**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
What is CURRENT_DATE? Shouldn't you be using CURRENT DATE?


I questioned that as well but found examples using CURRENT_DATE and
CURRENT DATE so I'm a little confused.

After checking the SQL Reference, CURRENT_DATE is also acceptable.
It will run faster if you skip the conversion to DATE on both sides of

the
equation.

So I don't need to convert the timestamp value to a date using the DATE
function?

If you use (CURRENT TIMESTAMP - 21 days) it will work.
Nov 12 '05 #4

<br**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
What is CURRENT_DATE? Shouldn't you be using CURRENT DATE?


I questioned that as well but found examples using CURRENT_DATE and
CURRENT DATE so I'm a little confused.

Traditionally, DB2 on OS/390 and Windows/Linux/Unix used 'CURRENT DATE' (no
underscore) but a few versions back, some of the platforms and versions
started accepting 'CURRENT_DATE' (with underscore). Perhaps this is some
kind of a standards comformance thing or an attempt to make DB2's syntax a
little closer to that of some competitor; I really don't know. In any case,
I suspect that most current platforms and versions support both formats and
that they are completely interchangeable. You haven't stated which platform
and version you are using so I can only suggest that you check your SQL
Reference to see which format(s) will work on your system.
It will run faster if you skip the conversion to DATE on both sides of

the
equation.

So I don't need to convert the timestamp value to a date using the DATE
function?

I think Mark is suggesting you do this:

Delete from LOG_TABLE
where LOG_TS < CURRENT_TIMESTAMP - 21 DAYS;

On any reasonably current version of DB2 on any platform, you should be able
to subtract 21 days from a timestamp without difficulty. Then you avoid
having the date() scalar function on both sides of the predicate which, as
Mark says, should give you better performance.

For what it's worth, I wrote this little script and executed it on my system
(DB2 UDB V8.2 FP8 on Windows XP) and it worked fine; the only three records
returned by the select were the ones that were 21 days old or older:

----------------------------------------------------------------------------
---
drop table log_table;
create table log_table
(log_ts timestamp not null,
entry char(20) not null,
primary key(log_ts));

insert into log_table values
(current timestamp + 3 days, '3 days from now'),
(current timestamp + 2 days, '2 days from now'),
(current timestamp + 1 day, 'tomorrow'),
(current timestamp, 'today'),
(current timestamp - 3 days, '3 days ago'),
(current timestamp - 7 days, '7 days ago'),
(current timestamp - 14 days, '14 days ago'),
(current timestamp - 21 days, '21 days ago'),
(current timestamp - 22 days, '22 days ago'),
(current timestamp - 24 days, '24 days ago');

select * from log_table;

select * from log_table
where log_ts < current timestamp - 21 days;
----------------------------------------------------------------------------
---

The only reason I can see for you getting only newer records is that your
operator is the reverse of the one you've shown in your question, which
would be a simple typo/transcription error.

Rhino
Nov 12 '05 #5
"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message news:VPNfe.16143
Traditionally, DB2 on OS/390 and Windows/Linux/Unix used 'CURRENT DATE'
(no
underscore) but a few versions back, some of the platforms and versions
started accepting 'CURRENT_DATE' (with underscore). Perhaps this is some
kind of a standards comformance thing or an attempt to make DB2's syntax a
little closer to that of some competitor; I really don't know. In any
case,
I suspect that most current platforms and versions support both formats
and
that they are completely interchangeable. You haven't stated which
platform
and version you are using so I can only suggest that you check your SQL
Reference to see which format(s) will work on your system.

The underscore version is for compliance with the the SQL 1999 Core
standard.
Nov 12 '05 #6

"Mark A" <no****@nowhere.com> wrote in message
news:hs********************@comcast.com...
"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message news:VPNfe.16143
Traditionally, DB2 on OS/390 and Windows/Linux/Unix used 'CURRENT DATE'
(no
underscore) but a few versions back, some of the platforms and versions
started accepting 'CURRENT_DATE' (with underscore). Perhaps this is some
kind of a standards comformance thing or an attempt to make DB2's syntax a little closer to that of some competitor; I really don't know. In any
case,
I suspect that most current platforms and versions support both formats
and
that they are completely interchangeable. You haven't stated which
platform
and version you are using so I can only suggest that you check your SQL
Reference to see which format(s) will work on your system.

The underscore version is for compliance with the the SQL 1999 Core
standard.

Okay, now I know for sure ;-)

Thanks; I don't really follow the various standards very closely.

Rhino
Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
4
by: Matt | last post by:
Hello. I have an Informix SQL statement that I need to run in MS SQL Server. When I try to execute it I get the following error message from Query Analyzer: Server: Msg 195, Level 15, State 10,...
8
by: Dennis M. Marks | last post by:
What is the maximum valid date range for the date(yyyy,mm,dd) function. -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with dcsi.net -----= Posted via...
5
by: Dennis M. Marks | last post by:
After reading section 15.9.1.1 the ECMAScript Language Specifications I see that the date range for the Date function is +/- 100,000,000 days from 01 Jan 1970. This is called an extrapolated...
6
by: Bill R via AccessMonster.com | last post by:
I have a query: SELECT tblCalendar.CalendarDay AS LastSunday FROM tblCalendar WHERE (((tblCalendar.CalendarDay)>=(Now()-7) And (tblCalendar.CalendarDay)...
4
by: cheryl | last post by:
I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the user will check first the room availability. The...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
1
by: sevak316 | last post by:
Hi, I would like to compare 2 dates and see which one is older using XQuery. Anyone? -Thanks.
2
by: dragrid | last post by:
Hi anyone - appreciate any help I would like to get * one column * with that min of the day instead of hr and min separately So the first line for example 18:50 pm would be the 1130 min of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.