Thread: How to Convert VarChar to Date in PgSQL

How to Convert VarChar to Date in PgSQL

From
"Venkat Rao Tammineni"
Date:

Dear All,

 

 

  I have one table which has lot of data.In the same table I have one varchar filed. I want to convert into Date data type? Is It possible to convert varchar to date datatype with out loosing data.Please guide me.I am waiting for your great response.

 

 

 

?Thanx & Regards

 Venkat Rao Tammineni

GIS Developer

 

Re: [GENERAL] How to Convert VarChar to Date in PgSQL

From
Raymond O'Donnell
Date:
On 10/03/2009 12:07, Venkat Rao Tammineni wrote:
>   I have one table which has lot of data.In the same table I have one
> varchar filed. I want to convert into Date data type? Is It possible to
> convert varchar to date datatype with out loosing data.Please guide me.I am
> waiting for your great response.

If the varchar is already in the format you need (yyyy-mm-dd) then you
ought to be able just to cast it:

   '2009-03-10'::date

If not, you may need to do some clever things with regular expressions
first to get it into this format, and then cast it.

Ray.

------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------

Re: How to Convert VarChar to Date in PgSQL

From
Jaume Sabater
Date:
On Tue, Mar 10, 2009 at 1:07 PM, Venkat Rao Tammineni
<vtammineni@roulacglobal.com> wrote:

>   I have one table which has lot of data.In the same table I have one
> varchar filed. I want to convert into Date data type? Is It possible to
> convert varchar to date datatype with out loosing data.Please guide me.I am
> waiting for your great response.

An easy solution would be to add a new column to the table, of type
date, and then execute an update statement that reads, for each row,
the value in the varchar field and writes the converted value to the
date field.

--
Jaume Sabater
http://linuxsilo.net/

"Ubi sapientas ibi libertas"

Re: [GENERAL] How to Convert VarChar to Date in PgSQL

From
Pavel Stehule
Date:
hello

2009/3/10 Venkat Rao Tammineni <vtammineni@roulacglobal.com>:
> Dear All,
>
>
>
>
>
>   I have one table which has lot of data.In the same table I have one
> varchar filed. I want to convert into Date data type? Is It possible to
> convert varchar to date datatype with out loosing data.Please guide me.I am
> waiting for your great response.
>
>
>

use function to_date, please

 http://www.postgresql.org/docs/8.3/static/functions-formatting.html

postgres=# select to_date('2009-07-17', 'YYYY-MM-DD');
 to_date
----------
2009-07-17
(1 row)

regards
Pavel Stehule

>
>
>
>
> ?Thanx & Regards
>
>  Venkat Rao Tammineni
>
> GIS Developer
>
>

Re: [GENERAL] How to Convert VarChar to Date in PgSQL

From
Pavel Stehule
Date:
2009/3/10 Jaume Sabater <jsabater@gmail.com>:
> On Tue, Mar 10, 2009 at 1:07 PM, Venkat Rao Tammineni
> <vtammineni@roulacglobal.com> wrote:
>
>>   I have one table which has lot of data.In the same table I have one
>> varchar filed. I want to convert into Date data type? Is It possible to
>> convert varchar to date datatype with out loosing data.Please guide me.I am
>> waiting for your great response.
>
> An easy solution would be to add a new column to the table, of type
> date, and then execute an update statement that reads, for each row,
> the value in the varchar field and writes the converted value to the
> date field.
>

resp.
alter table someatb alter column columname type date using
to_data(columname, 'YYYY-MM-DD');

regards
Pavel

> --
> Jaume Sabater
> http://linuxsilo.net/
>
> "Ubi sapientas ibi libertas"
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>