How to Convert VarChar to Date in PgSQL

Started by Venkat Rao Tammineniabout 17 years ago6 messagesgeneral
Jump to latest
#1Venkat 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.

?Thanx & Regards

Venkat Rao Tammineni

GIS Developer

In reply to: Venkat Rao Tammineni (#1)
Re: [GENERAL] How to Convert VarChar to Date in PgSQL

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
------------------------------------------------------------------

#3Jaume Sabater
jsabater@gmail.com
In reply to: Venkat Rao Tammineni (#1)
Re: How to Convert VarChar to Date in PgSQL

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"

#4A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Venkat Rao Tammineni (#1)
Re: How to Convert VarChar to Date in PgSQL

In response to Venkat Rao Tammineni :

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.

Sure, if you have valid varchar-data.

to_date('2009-03-11'::text, 'yyyy-mm-dd');

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: Venkat Rao Tammineni (#1)
Re: [GENERAL] How to Convert VarChar to Date in PgSQL

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

Show quoted text

?Thanx & Regards

 Venkat Rao Tammineni

GIS Developer

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Jaume Sabater (#3)
Re: [GENERAL] How to Convert VarChar to Date in PgSQL

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

Show quoted text

--
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