473,387 Members | 1,722 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,387 software developers and data experts.

Date manipulation

Hi all,
I want to write a function in pgsql that given a number like 7 turns
that into a date, 7 weeks in the past. I have a table that is
essentially logging some information and want to write a trigger that is
data driven in trimming old values out of the log, that way if I thought
that 4 weeks was sufficient, I could adjust the value.

I've had a quick look around the docs and they're fairly spartan on
details like this.

Can anybody point me to an online reference for pgsql overall and
something that talks about the pitfalls and gives code examples of this
kind of thing?

Regards.
Hadley

Nov 23 '05 #1
6 2974
On Mon, Apr 19, 2004 at 09:02:35AM +1200, Hadley Willan wrote:
I want to write a function in pgsql that given a number like 7 turns
that into a date, 7 weeks in the past. I have a table that is
essentially logging some information and want to write a trigger that is
data driven in trimming old values out of the log, that way if I thought
that 4 weeks was sufficient, I could adjust the value.


Just use the - operator ...

alvherre=# select '2004-01-01'::date - 7 * '1 week'::interval;
?column?
---------------------
2003-11-13 00:00:00
(1 fila)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Hay que recordar que la existencia en el cosmos, y particularmente la
elaboración de civilizaciones dentre de él no son, por desgracia,
nada idílicas" (Ijon Tichy)

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #2
Thanks, that's pretty easy.

I take it I could just use ''now'' instead of a date

dateRange = ''now''::date - ( 7 * '1 week'::interval );

Hadley

On Mon, 2004-04-19 at 10:36, Alvaro Herrera wrote:
On Mon, Apr 19, 2004 at 09:02:35AM +1200, Hadley Willan wrote:
I want to write a function in pgsql that given a number like 7 turns
that into a date, 7 weeks in the past. I have a table that is
essentially logging some information and want to write a trigger that is
data driven in trimming old values out of the log, that way if I thought
that 4 weeks was sufficient, I could adjust the value.


Just use the - operator ...

alvherre=# select '2004-01-01'::date - 7 * '1 week'::interval;
?column?
---------------------
2003-11-13 00:00:00
(1 fila)


Nov 23 '05 #3
On Mon, Apr 19, 2004 at 11:18:23 +1200,
Hadley Willan <ha***********@deeperdesign.co.nz> wrote:
Thanks, that's pretty easy.

I take it I could just use ''now'' instead of a date

dateRange = ''now''::date - ( 7 * '1 week'::interval );


If you actually are using a date you don't have to use an interval as
the number subtracted will be in days. So you could just use
current_date - (7 * 7);

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #4
Hadley Willan <ha***********@deeperdesign.co.nz> writes:
I take it I could just use ''now'' instead of a date
dateRange = ''now''::date - ( 7 * '1 week'::interval );


You probably want to use the SQL-standard spelling:

dateRange = current_date - ( 7 * '1 week'::interval );

Aside from being standard, this doesn't pose risk of the constant being
reduced sooner than you want. The other way is likely to break if the
plpgsql function gets cached over more than a day.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5
Bruno Wolff III <br***@wolff.to> writes:
Hadley Willan <ha***********@deeperdesign.co.nz> wrote:
dateRange = ''now''::date - ( 7 * '1 week'::interval );
If you actually are using a date you don't have to use an interval as
the number subtracted will be in days. So you could just use
current_date - (7 * 7);


Good point --- in fact, that's probably exactly what Hadley wants,
because "date - integer" will yield a date and not a timestamp. The
form involving interval will do odd things when crossing a DST
transition.

regards, tom lane

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

Nov 23 '05 #6
Yes, I'm not worried about an exact DST. The date items are stored with
Timestamp Timezone.

Thanks to everybody for your technical tips and help.

Hadley

On Mon, 2004-04-19 at 16:42, Tom Lane wrote:
Bruno Wolff III <br***@wolff.to> writes:
Hadley Willan <ha***********@deeperdesign.co.nz> wrote:
dateRange = ''now''::date - ( 7 * '1 week'::interval );

If you actually are using a date you don't have to use an interval as
the number subtracted will be in days. So you could just use
current_date - (7 * 7);


Good point --- in fact, that's probably exactly what Hadley wants,
because "date - integer" will yield a date and not a timestamp. The
form involving interval will do odd things when crossing a DST
transition.

regards, tom lane


Nov 23 '05 #7

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

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
16
by: jason | last post by:
I'm tearing out my hair here: ACCESS 2000: When I attempt to overwrite a date in my date field with a new user selected valid date eg: 05/23/99 my date field changes to the TIME I updated the...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
5
by: John Kiernan | last post by:
Hey JavaScript gurus! Here's a weird one: If I check (with an alert) this line: vStrDate2 = vDate.toLocaleString(); I get: Saturday, August 14, 2004 7:57:30 PM (as I should) but this...
26
by: sgershon | last post by:
Hi. I know this is should be a simple question. I know server-side web-programming, and never needed to use client-side scripting... until now :) I have done so far a little number of scripts...
5
by: WindAndWaves | last post by:
Hi Gurus This is a cry for help to all of you. I am looking for some sort of plug-in/active X/code/link to outlook/what have you, which I can add to my MS Access database for my users to...
1
by: Graham Feeley | last post by:
I have a date field named rcdate it is a general date eg: 12/08/2006 3:30:00 PM 12/08/2006 3:00:00 PM I used to update another empty field named rdate with the with using in a update query "...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
2
by: Billy | last post by:
This string is supposed to provide all records from an MDB database that match the courier and date specified in the query. I Response.Write the query and I get a date as 1/27/2007. The date...
3
by: GorseFox | last post by:
Forgive me. I am new to PHP and am a little confused by date manipulation and would appreciate some guidance. I have a string containing a date which has been parsed from and RSS feed. This looks...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.