Re: [GENERAL] Is there a better way to do this?

2007-08-29 Thread David Fetter
On Tue, Aug 28, 2007 at 04:59:46PM -0400, Wei Weng wrote: Hi all I want to implement something like the following: CREATE OR REPLACE FUNCTION AddDays (TIMESTAMP WITHOUT TIME ZONE , INT) RETURNS TIMESTAMP WITHOUT TIME ZONE AS ' DECLARE time ALIAS FOR $1;

[GENERAL] Is there a better way to do this?

2007-08-28 Thread Wei Weng
Hi all I want to implement something like the following: CREATE OR REPLACE FUNCTION AddDays (TIMESTAMP WITHOUT TIME ZONE , INT) RETURNS TIMESTAMP WITHOUT TIME ZONE AS ' DECLARE time ALIAS FOR $1; days ALIAS FOR $2; BEGIN RETURN time+days*24*3600*''1

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Martijn van Oosterhout
On Tue, Aug 28, 2007 at 04:59:46PM -0400, Wei Weng wrote: Hi all I want to implement something like the following: Well, you could always implement it as SQL instead (untested): CREATE OR REPLACE FUNCTION AddDays (TIMESTAMP WITHOUT TIME ZONE , INT) RETURNS TIMESTAMP WITHOUT

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 15:59 , Wei Weng wrote: I don't really like this implementation. Is there a more concise way to do this? create or replace function add_days(timestamp, int) returns timestamp language sql as $body$ select $1 + $2 * interval '1 day' $body$; Note that interval '1 day' is

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Osvaldo Rosario Kussama
Wei Weng escreveu: Hi all I want to implement something like the following: CREATE OR REPLACE FUNCTION AddDays (TIMESTAMP WITHOUT TIME ZONE , INT) RETURNS TIMESTAMP WITHOUT TIME ZONE AS ' DECLARE time ALIAS FOR $1; days ALIAS FOR $2; BEGIN RETURN

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread D. Dante Lorenso
Wei Weng wrote: I want to implement something like the following: CREATE OR REPLACE FUNCTION AddDays You don't know how many seconds are in a day, so just add the days using SQL. RETURN time + (days || ' days')::INTERVAL; You don't even need to make that a function, just do that

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Rodrigo De León
On 8/28/07, Wei Weng [EMAIL PROTECTED] wrote: Is there a more concise way to do this? CREATE OR REPLACE FUNCTION ADDDAYS (TIMESTAMP WITHOUT TIME ZONE, INT) RETURNS TIMESTAMP WITHOUT TIME ZONE AS ' SELECT $1+($2 * ''1 DAY''::INTERVAL) ' LANGUAGE SQL; ---(end of

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 16:51 , Michael Glaesemann wrote: If you mean 24 hours (which you're getting with your 24 * 3600 * interval '2 second'), you could do Or, 24 * 3600 * interval '1 second', rather Michael Glaesemann grzm seespotcode net ---(end of

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote: RETURN time + (days || ' days')::INTERVAL; It's bad practice to concatenate like this. Use time + days * interval '1 day' and be done with it. Michael Glaesemann grzm seespotcode net ---(end of

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread D. Dante Lorenso
Michael Glaesemann wrote: On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote: RETURN time + (days || ' days')::INTERVAL; It's bad practice to concatenate like this. Use time + days * interval '1 day' and be done with it. Why? Is this functionality expected to break in the future or has

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 17:22 , D. Dante Lorenso wrote: Michael Glaesemann wrote: On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote: RETURN time + (days || ' days')::INTERVAL; It's bad practice to concatenate like this. Use time + days * interval '1 day' and be done with it. Why? Is

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 17:46 , Michael Glaesemann wrote: I'm sure others could provide more cogent explanations, but those are my initial thoughts. Thinking about this a little bit more: pushing interpolation/ concatenation to the furthest extreme you get to using eval-like construct, which

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread D. Dante Lorenso
Michael Glaesemann wrote: On Aug 28, 2007, at 17:22 , D. Dante Lorenso wrote: Michael Glaesemann wrote: On Aug 28, 2007, at 16:55 , D. Dante Lorenso wrote: RETURN time + (days || ' days')::INTERVAL; It's bad practice to concatenate like this. Use time + days * interval '1 day' and be

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Tom Lane
Michael Glaesemann [EMAIL PROTECTED] writes: Note that interval '1 day' is not equal to interval '24 hours'. '1 day' can be 23 or 25 hours across daylight saving time boundaries. When you are adding to timestamp without time zone, they *are* interchangeable, since no daylight-savings

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Michael Glaesemann
On Aug 28, 2007, at 19:30 , Tom Lane wrote: Michael Glaesemann [EMAIL PROTECTED] writes: Note that interval '1 day' is not equal to interval '24 hours'. '1 day' can be 23 or 25 hours across daylight saving time boundaries. When you are adding to timestamp without time zone, they *are*

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Tom Lane
Michael Glaesemann [EMAIL PROTECTED] writes: The concatenation is probably less efficient: you're casting an int to text and then the text to interval with the concatenation you're using. I don't know how that compares in terms of cycles to the int * interval math, but efficiency isn't

Re: [GENERAL] Is there a better way to do this?

2007-08-28 Thread Ron Johnson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 08/28/07 20:06, Tom Lane wrote: [snip] As Michael says, the speed argument is really kinda minor compared to the other ones, but it's real enough. Every little bit counts, though. For example, if it's part of an otherwise