473,508 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with syntax for timestamp addition

New to Postgres 7.3 from 7.0.

Am having some trouble with a query that worked in 7.0 but not in
7.3.....can't seem to figure out the syntax or find info about how to do
this anywhere.

Consider for the following query:
- 'number' is an integer
- 'procedures' is the table name
- 'date' is a timestamp
- 'numdays' is an integer

SELECT number
FROM procedures
WHERE date + numdays <= CURRENT_TIMESTAMP;

In 7.0 this works with no problem...finding all rows where the date plus
some number of days is less than the current. But in 7.3 I get:

ERROR: Unable to identify an operator '+' for types 'timestamp without
time zone' and 'integer'
You will have to retype this query using an explicit cast
I've never had to create casts before so I'm not too sure how to work
this casting into the query....keep getting various syntax errors no
matter what I try. If I try to incorporate intervals, I also get errors.
I just can't seem to find good examples in any documentation.

Any help is appreciated.

-Scott

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
6 6292
Have a try at this syntax

SELECT number
FROM procedures
WHERE date + CAST( numdays || ' days' AS interval ) <= CURRENT_TIMESTAMP;

Patrick
-------------------------------------------------------------------------- ----------------- Patrick Fiche
email : pa***********@aqsacom.com
tél : 01 69 29 36 18
-------------------------------------------------------------------------- -----------------

-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org]On Behalf Of Scott Nixon
Sent: lundi 22 novembre 2004 14:56
To: pg***********@postgresql.org
Subject: [GENERAL] Help with syntax for timestamp addition
New to Postgres 7.3 from 7.0.

Am having some trouble with a query that worked in 7.0 but not in
7.3.....can't seem to figure out the syntax or find info about how to do
this anywhere.

Consider for the following query:
- 'number' is an integer
- 'procedures' is the table name
- 'date' is a timestamp
- 'numdays' is an integer

SELECT number
FROM procedures
WHERE date + numdays <= CURRENT_TIMESTAMP;

In 7.0 this works with no problem...finding all rows where the date plus
some number of days is less than the current. But in 7.3 I get:

ERROR: Unable to identify an operator '+' for types 'timestamp without
time zone' and 'integer'
You will have to retype this query using an explicit cast
I've never had to create casts before so I'm not too sure how to work
this casting into the query....keep getting various syntax errors no
matter what I try. If I try to incorporate intervals, I also get errors.
I just can't seem to find good examples in any documentation.

Any help is appreciated.

-Scott

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Protected by Polesoft Lockspam
http://www.polesoft.com/refer.html
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
On Mon, 22 Nov 2004 15:12:26 +0100, Patrick Fiche
<pa***********@aqsacom.com> wrote:
Have a try at this syntax

SELECT number
FROM procedures
WHERE date + CAST( numdays || ' days' AS interval ) <= CURRENT_TIMESTAMP;


Just for the record you could write it like this too:
SELECT number
FROM procedures
WHERE date + (numdays || ' days')::interval <= CURRENT_TIMESTAMP;

Ian Barwick

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3
Ian Barwick wrote:
On Mon, 22 Nov 2004 15:12:26 +0100, Patrick Fiche

<pa***********@aqsacom.com> wrote:
Have a try at this syntax

SELECT number
FROM procedures
WHERE date + CAST( numdays || ' days' AS interval ) <=
CURRENT_TIMESTAMP;


Just for the record you could write it like this too:
SELECT number
FROM procedures
WHERE date + (numdays || ' days')::interval <= CURRENT_TIMESTAMP;


Just to add to the record, the mathematically sound way to write this
query would be this:

SELECT number
FROM procedures
WHERE date + numdays * interval '1 day' <= current_timestamp;

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
> Just to add to the record, the mathematically sound way to write this
query would be this:

SELECT number
FROM procedures
WHERE date + numdays * interval '1 day' <= current_timestamp;

Thanks for that Peter! That's a lot closer than what I originally
had...I didn't think about doing that but it makes sense.
Is there any advantage/disadvantages to using this method or the other?


On Mon, 2004-11-22 at 10:26, Peter Eisentraut wrote: Ian Barwick wrote:
On Mon, 22 Nov 2004 15:12:26 +0100, Patrick Fiche

<pa***********@aqsacom.com> wrote:
Have a try at this syntax

SELECT number
FROM procedures
WHERE date + CAST( numdays || ' days' AS interval ) <=
CURRENT_TIMESTAMP;


Just for the record you could write it like this too:
SELECT number
FROM procedures
WHERE date + (numdays || ' days')::interval <= CURRENT_TIMESTAMP;


Just to add to the record, the mathematically sound way to write this
query would be this:

SELECT number
FROM procedures
WHERE date + numdays * interval '1 day' <= current_timestamp;

--
Peter Eisentraut
http://developer.postgresql.org/~petere/



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
Scott Nixon <sn****@lssi.net> writes:
Am having some trouble with a query that worked in 7.0 but not in
7.3.....can't seem to figure out the syntax or find info about how to do
this anywhere. SELECT number
FROM procedures
WHERE date + numdays <= CURRENT_TIMESTAMP; In 7.0 this works with no problem...


(Tries it...) The reason it worked in 7.0 was that 7.0 would implicitly
down-convert the timestamp value to a value of type date, and then apply
the date-plus-integer operator. The operator is still there, but later
versions are less willing to apply information-losing type coercions
implicitly. So the exact equivalent of what you were doing before is

.... WHERE CAST(date AS date) + numdays <= CURRENT_TIMESTAMP;

The comparison portion of this will require an up-conversion from date
back to timestamp, which is inefficient and pointless (not to mention
that it exposes you to daylight-savings-transition issues, because
CURRENT_TIMESTAMP is timestamp with time zone). So I think what you
probably *really* want is

.... WHERE CAST(date AS date) + numdays <= CURRENT_DATE;

which keeps both the addition and the comparison as simple date
operations with no sub-day resolution and no timezone funnies.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #6
So I think what you probably *really* want is

... WHERE CAST(date AS date) + numdays <= CURRENT_DATE;

Thanks Tom! Yeah, I guess you are right on that point. I hadn't thought about that.
The implementation of this wouldn't be affected since this query is buried in a script
that runs out of cron once a day, but I suppose I might as well do it right if I'm
going to do it.

On Mon, 2004-11-22 at 11:31, Tom Lane wrote: Scott Nixon <sn****@lssi.net> writes:
Am having some trouble with a query that worked in 7.0 but not in
7.3.....can't seem to figure out the syntax or find info about how to do
this anywhere.

SELECT number
FROM procedures
WHERE date + numdays <= CURRENT_TIMESTAMP;

In 7.0 this works with no problem...


(Tries it...) The reason it worked in 7.0 was that 7.0 would implicitly
down-convert the timestamp value to a value of type date, and then apply
the date-plus-integer operator. The operator is still there, but later
versions are less willing to apply information-losing type coercions
implicitly. So the exact equivalent of what you were doing before is

... WHERE CAST(date AS date) + numdays <= CURRENT_TIMESTAMP;

The comparison portion of this will require an up-conversion from date
back to timestamp, which is inefficient and pointless (not to mention
that it exposes you to daylight-savings-transition issues, because
CURRENT_TIMESTAMP is timestamp with time zone). So I think what you
probably *really* want is

... WHERE CAST(date AS date) + numdays <= CURRENT_DATE;

which keeps both the addition and the comparison as simple date
operations with no sub-day resolution and no timezone funnies.

regards, tom lane

--
______________________________________
D. Scott Nixon

LSSi Corp.
email: ni***@lssi.net
url: http://www.lssi.net/~snixon
phone: (919) 466-6834
fax: (919) 466-6810
______________________________________
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #7

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

Similar topics

1
2029
by: roll | last post by:
Hi! I have to get the difference between date 1 and date 2 (that is date 2 minus date 1) ; the format is timestamp. How can I do it? Does anybody have an example of code? Thank you.. Pero
2
1380
by: Jason | last post by:
Hello, I've got an example from the mingw website of creating a dll. It is 3 files: a header, .c file and another file containing main. I want to use the dll in VB and it works for tstfunc, but I...
2
1617
by: akickdoe22 | last post by:
i could really use help finishing this addition program. I'm stuck on the part that allows you to add any two large integers,up to 100 digits,(pos+pos, neg+neg, and pos+neg). could use hints ideas...
1
2968
by: George Develekos | last post by:
I need to import into mysql data from DB2. One of the DB2 table columns is of the TIMESTAMP type, which, unlike its MySQL counterpart, supports fractions of a second, up to 6 digits, i.e. it is of...
5
5356
by: pankaj_wolfhunter | last post by:
Greetings, In Sybase, a timestamp column is automatically loaded by the DB engine and its in some hex format. This is shown below id timestamp_value ----------- ------------------ 1...
6
1369
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
2
5726
by: Bob Alston | last post by:
I am going blind tonight but I cannot figure out the error. I get a syntax error from this sql statement, being run via vba in access 2003 insert into tbl_Volunteer_Donor in...
9
1968
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
2
1634
by: jiexian | last post by:
Please teach me how to do this the right way in vb6 or vb08. (preferbably vb6) What i want is to increase the value of Text1.text by 1 every time i click it. But with this code, Text1.text remains...
0
7115
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
7321
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
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.