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

How to convert date format in PostgreSQL

Chittaranjan
Hi All,

I am not sure how to change the date format in PostgreSQL. I have the dates stored in the database as
Expand|Select|Wrap|Line Numbers
  1. yyyy-mm-dd HH:MM:SS+TimeZone to get the GMT time (Ex: 2008-02-28 14:03:23+05:30)
. But I want these dates to be shown in the following format:
Expand|Select|Wrap|Line Numbers
  1. dd/mm/yyyy HH:MM:SS TimeZone (Ex: 28/02/2008 14:03:23 IST)
So now while I am trying to fetch the dates from the database I am using to_char() as below:
Expand|Select|Wrap|Line Numbers
  1. to_char(field_name, 'DD/MM/YYYY HH24:MI:SS TZ') AS field_name
In this way I am able to get the required format but the problem I am facing is when I am trying to use this information then I think it is treating the dates as characters so I am not able to get required result.
For Ex: When I am trying to sort the output records with the dates then it is not sorting properly.

So can anybody give some idea or example how to do this in other way and get rid of my issue.

Thanks and Regards,
Chittaranjan :)
Feb 28 '08 #1
3 24181
rski
700 Expert 512MB
Hi All,

I am not sure how to change the date format in PostgreSQL. I have the dates stored in the database as yyyy-mm-dd HH:MM:SS+TimeZone to get the GMT time (Ex: 2008-02-28 14:03:23+05:30). But I want these dates to be shown in the following format:
dd/mm/yyyy HH:MM:SS TimeZone (Ex: 28/02/2008 14:03:23 IST). So now while I am trying to fetch the dates from the database I am using to_char() as below:
to_char(field_name, 'DD/MM/YYYY HH24:MI:SS TZ') AS field_name

In this way I am able to get the required format but the problem I am facing is when I am trying to use this information then I think it is treating the dates as characters so I am not able to get required result.
For Ex: When I am trying to sort the output records with the dates then it is not sorting properly.

So can anybody give some idea or example how to do this in other way and get rid of my issue.

Thanks and Regards,
Chittaranjan :)
Is this
Expand|Select|Wrap|Line Numbers
  1. set datestyle to 'sql,european';
  2.  
what you are looking for?
Or you can set
Expand|Select|Wrap|Line Numbers
  1. alter user username set datestyle to 'sql,european';
  2.  
to save the setting for the next time you connect to postgres
Feb 28 '08 #2
Is this
Expand|Select|Wrap|Line Numbers
  1. set datestyle to 'sql,european';
  2.  
what you are looking for?
Or you can set
Expand|Select|Wrap|Line Numbers
  1. alter user username set datestyle to 'sql,european';
  2.  
to save the setting for the next time you connect to postgres
Hi rski,

Thanks a lot for your quick response. The examples you provided are very valuable but this is for changing the entire date style of the database tables. But I want to change the date format to European style while fetching the records from the database to show on the front end.

Please let me know if you or any one had this this kind of problem ever or the solution for this.

Your valuable responses are highly appreciated.

Thanks and Regards,
Chittaranjan :)
Feb 29 '08 #3
rski
700 Expert 512MB
Hi rski,

Thanks a lot for your quick response. The examples you provided are very valuable but this is for changing the entire date style of the database tables. But I want to change the date format to European style while fetching the records from the database to show on the front end.

Please let me know if you or any one had this this kind of problem ever or the solution for this.

Your valuable responses are highly appreciated.

Thanks and Regards,
Chittaranjan :)
Maybe write a plpgsql function like that
Expand|Select|Wrap|Line Numbers
  1. create or replace function date_convert(varchar) returns date as
  2. $$
  3. declare
  4. begin
  5. execute 'set datestyle to '||'' || $1||'';
  6. return now()::date;
  7. end;
  8. $$ language 'plpgsql'
  9.  
and call it in php script for example
Expand|Select|Wrap|Line Numbers
  1. $query="select date_convert('german')";
  2. $result=pg_query($connect,$query);
  3.  
is it helpful?
Mar 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
3
by: Netto | last post by:
The way PostgreSQL deals with the date format is confusing me... I need PostgreSQL to return dates from selects at this format: "dd/mm/yyyy", but it insists in returning it as "yyyy-mm-dd". I say...
2
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings...
1
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as...
9
by: David Rysdam | last post by:
I have a large amount of data that I copy in and out of Sybase very often. Now I also want to copy this data in and out of postgres. I have an existing script that creates the entire database(s)...
1
by: somaskarthic | last post by:
Hi In postgresql database how to retrieve the date in a required format ? Is ther any method to retrieve the stored date value in mm/dd/yyyy format. I don't want to set the date format in...
2
by: thewilldog | last post by:
Hello, I've reviewed the archives here to address the issue, but I'm still running into problems. I've got a table field populated with the record date in text "YYYYMMDD" To convert it into a...
4
by: Ashraf Ansari | last post by:
Hi, How Can I convert MM/dd/yyyy format into dd/MM/yyyy and the date which is converted into dd/MM/yyyy should be in DateTime format. I do not want to store it as a string. Please help ...
4
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.