Hello,
I know that PostgreSQL, like most database management systems, has a
function
call called NOW() that returns the current date. Is there a way to
return a datein PostgreSQL such that the output is in ISO 8601 format
(Unix 'date -I' format)but such that the date is not "today"'s date
but the date two days ago or five
days ahead of now? I have tried something like NOW() + 5 but that did
not work
(because the data types are incompatible, and SELECT NOW() +
'0000-00-01' does
not work either. I get the error:
ERROR: Bad interval external representation '0000-00-01'
Thanks,
Neil 6 10187
On Sat, Oct 25, 2003 at 09:35:35PM -0700, Neil Zanella wrote: Hello,
I know that PostgreSQL, like most database management systems, has a function call called NOW() that returns the current date. Is there a way to return a datein PostgreSQL such that the output is in ISO 8601 format (Unix 'date -I' format)but such that the date is not "today"'s date but the date two days ago or five days ahead of now?
Certainly. Try the following:
SELECT now() + 5 * '1 day'::interval;
Or, more verbose,
SELECT now() + 5 * CAST('1 day' AS interval);
You can of course do
SELECT now() + CAST('5 day' AS interval);
But the two previous examples can be more easily constructed in an SQL o
PL/pgSQL function.
For the date -I format you can use something like
SELECT to_char(now() + 5 * '1 day'::interval, 'YYYY-MM-DD');
--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La tristeza es un muro entre dos jardines" (Khalil Gibran)
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives? http://archives.postgresql.org
On Sat, 25 Oct 2003, Neil Zanella wrote: Hello,
I know that PostgreSQL, like most database management systems, has a function call called NOW() that returns the current date. Is there a way to return a datein PostgreSQL such that the output is in ISO 8601 format (Unix 'date -I' format)but such that the date is not "today"'s date but the date two days ago or five days ahead of now? I have tried something like NOW() + 5 but that did not work (because the data types are incompatible, and SELECT NOW() + '0000-00-01' does not work either. I get the error:
If you want a date, I'd suggest something like
CURRENT_DATE+5
The reason this works while, now()+5 doesn't is that now() doesn't return
a date, but a timestamp type (including time).
If you want time information, then probably
CURRENT_TIMESTAMP + INTERVAL '5 days'
---------------------------(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
Postgres has a type called "interval" to deal with these kinds of
situations. As you might expect, an interval is a duration of time, as
opposed to a fixed time ordinate like date or timestamp. You can add
and subtract interval values from dates and timestamps. Intervals need
to be specified as strings and then cast to interval. You can do this
in two ways:
interval '5 days'
'5 days'::interval
So to get "five days ago", you would use
now() - interval '5 days'
Similarly, to get 40 minutes into the future
now() + interval '40 minutes'
The resultant date or timestamp value can then be expressed in whatever
format you please by using to_char()
BJ
Neil Zanella wrote: Hello,
I know that PostgreSQL, like most database management systems, has a function call called NOW() that returns the current date. Is there a way to return a datein PostgreSQL such that the output is in ISO 8601 format (Unix 'date -I' format)but such that the date is not "today"'s date but the date two days ago or five days ahead of now? I have tried something like NOW() + 5 but that did not work (because the data types are incompatible, and SELECT NOW() + '0000-00-01' does not work either. I get the error:
ERROR: Bad interval external representation '0000-00-01'
Thanks,
Neil
---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend
---------------------------(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
BlakJak <bl*****@blakjak.sytes.net> writes: So to get "five days ago", you would use now() - interval '5 days'
Actually, given that the OP seems to only want a date result and not a
time-of-day, I'd suggest something like
current_date - 5
The date-plus-integer and date-minus-integer operators do exactly what
I think is being asked for. timestamp-minus-interval does computations
including fractional days, which will just confuse matters
.... especially near DST transition days. For instance, right now I get
regression=# select now();
now
-------------------------------
2003-10-27 01:45:14.458268-05
(1 row)
regression=# select now() - interval '5 days';
?column?
------------------------------
2003-10-22 02:45:20.22788-04
(1 row)
which is correct in one sense but is surely going to confuse some
people.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend al******@dcc.uchile.cl (Alvaro Herrera) wrote in message Certainly. Try the following: SELECT now() + 5 * '1 day'::interval;
Or, more verbose, SELECT now() + 5 * CAST('1 day' AS interval);
You can of course do SELECT now() + CAST('5 day' AS interval);
But the two previous examples can be more easily constructed in an SQL or PL/pgSQL function.
Perhaps I should get myself a copy of the relevant parts of the SQL 99 standard.
How would you do the above in standard SQL?
For the date -I format you can use something like SELECT to_char(now() + 5 * '1 day'::interval, 'YYYY-MM-DD');
I believe Oracle also has a to_char() function. Is this to_char() function
part of standard SQL or is it just a coincidence that both DBMSs support
such a function call? I wonder whether the PostgreSQL to_char()
function is compatible with the Oracle one.
Thanks,
Neil
On Mon, Oct 27, 2003 at 01:40:53AM -0800, Neil Zanella wrote: al******@dcc.uchile.cl (Alvaro Herrera) wrote in message
You can of course do SELECT now() + CAST('5 day' AS interval);
Perhaps I should get myself a copy of the relevant parts of the SQL 99 standard. How would you do the above in standard SQL?
I think one standard way of doing the above would be
SELECT CURRENT_TIMESTAMP + CAST('5 day' AS interval);
Or, as pointed out by Tom Lane and someone else, if you don't need the
time part,
SELECT CURRENT_DATE + 5; For the date -I format you can use something like SELECT to_char(now() + 5 * '1 day'::interval, 'YYYY-MM-DD');
I believe Oracle also has a to_char() function. Is this to_char() function part of standard SQL or is it just a coincidence that both DBMSs support such a function call? I wonder whether the PostgreSQL to_char() function is compatible with the Oracle one.
AFAIK the main motivation to create the to_char() function in the first
place was in fact Oracle compatibility. If you want to do such a thing
in a standard manner, you should probably do
SELECT EXTRACT(year FROM a) || '-' ||
EXTRACT(month FROM a) || '-' ||
EXTRACT(day FROM a)
FROM (SELECT CURRENT_DATE + 5 AS a) AS foo;
--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"I would rather have GNU than GNOT." (ccchips, lwn.net/Articles/37595/)
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian |
last post by:
Here's the example:
You have a world writable file called lastquote.cfg on your server with a
one or two digit number in it. When you run the script below, it reads the
value, increments the...
|
by: Mike Brearley |
last post by:
I have a counter (alright one I found on asp101.com) that checks for a
session variable to prevent the counter from incrmenting if a user refreshes
the page or returns to the page during the same...
|
by: J. Campbell |
last post by:
Hi everyone. I'm sure that this is common knowledge for many/most
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm...
|
by: brian |
last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript /
JavaScript and also in my favorite text editor: TextPad (www.textpad.com)
I don't know about JScript/JavaScript, but...
|
by: pozz |
last post by:
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value...
|
by: Shannon Jacobs |
last post by:
The OL tag still allows for a START value, but that is now deprecated.
I've found sound references that suggest the proper technique now is
to control it with a style for the OL in quetion, but I...
|
by: jesmi |
last post by:
hi
i got problem in inserting the date into the database. my requirement is that when i choose a date ie from :2007-01-01 & to :2007-12-01 then all the dates starting from 2007-02-01 upto...
|
by: Bill Cunningham |
last post by:
I don't know if I'll need pointers for this or not. I wants numbers
10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
would be 10^16. So I have
int num ;
These are of...
|
by: Richard |
last post by:
I'm still battling with this causing UDB:
while(e-- s);
if s points to the start of a string and e becomes less than s then e is
not really pointing to defined char. Fine.
But UDB?
Yes,...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |