Re: [GENERAL] check data for datatype

From: Jerry Sievers <gsievers19(at)comcast(dot)net>
To: Suresh Raja <suresh(dot)rajaabc(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [GENERAL] check data for datatype
Date: 2015-03-27 18:53:09
Message-ID: 86vbhm2rxm.fsf@jerry.enova.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Suresh Raja <suresh(dot)rajaabc(at)gmail(dot)com> writes:

> Hi All:
>
> I have a very large table and the column type is text.  I would like to convert in numeric.  How can I find rows that dont have numbers.  I would like to delete those
> rows.

begin;

set local client_min_messages to notice;

create table foo (a text);
copy foo from stdin;
1
foo
\.

create function foo (text)
returns numeric
as $$
begin
return $1::numeric;
exception when invalid_text_representation then
raise notice '%: %', sqlstate, sqlerrm;
return 'nan';
end
$$
language plpgsql;

alter table foo alter a type numeric using foo(a);

select * from foo;

--now go delete your 'nan rows

abort;

>
> Thanks,
> -Suersh Raja
>

--
Jerry Sievers
Postgres DBA/Development Consulting
e: postgres(dot)consulting(at)comcast(dot)net
p: 312.241.7800

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Eli Murray 2015-03-27 19:30:13 Re: Building JSON objects
Previous Message Adrian Klaver 2015-03-27 18:51:20 Re: Building JSON objects

Browse pgsql-sql by date

  From Date Subject
Next Message avpro avpro 2015-03-30 08:20:15 Link Office Word form document with data from PostgreSQL
Previous Message Raymond O'Donnell 2015-03-27 18:14:50 Re: check data for datatype