Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 041EF1337B49 for ; Sat, 19 Feb 2011 07:45:15 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 73209-04 for ; Sat, 19 Feb 2011 11:45:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id D70021337B43 for ; Sat, 19 Feb 2011 07:45:06 -0400 (AST) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PqlFI-0001VI-6y for pgsql-sql@postgresql.org; Sat, 19 Feb 2011 12:45:04 +0100 Received: from ip-118-90-141-75.xdsl.xnet.co.nz ([118.90.141.75]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 19 Feb 2011 12:45:04 +0100 Received: from jasen by ip-118-90-141-75.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 19 Feb 2011 12:45:04 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: Determine length of numeric field Date: 15 Feb 2011 23:32:50 GMT Organization: Dis (not Dat) Organisation Lines: 55 Message-ID: References: <1297802915.1703.13.camel@tony1.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-141-75.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/104 X-Sequence-Number: 35180 On 2011-02-15, Tony Capobianco wrote: > I'm altering datatypes in several tables from numeric to integer. In > doing so, I get the following error: > > dw=# \d uniq_hits > Table "support.uniq_hits" > Column | Type | Modifiers > ------------+---------+----------- > sourceid | numeric | > hitdate | date | > total | numeric | > hitdate_id | integer | > Indexes: > "uniq_hits_hitdateid_idx" btree (hitdate_id), tablespace > "support_idx" > Tablespace: "support" > > esave_dw=# alter table uniq_hits alter sourceid type int; > ERROR: integer out of range > > Sourceid should not be more than 5 digits long. I'm able to perform > this query on Oracle and would like something similar on postgres 8.4: > > delete from uniq_hits where sourceid in (select sourceid from uniq_hits > where length(sourceid) > 5); delete from uniq_hits where sourceid in (select sourceid from uniq_hits where length(sourceid::text) > 5); or even: delete from uniq_hits where length(sourceid::text) > 5; but using length on numbers is usually the wrong way. do this instead: delete from uniq_hits where abs(sourceid) > 2^32-1; Which will hit all the ones that can't be converted. You may want to do a select first to see what you're deleting. > I haven't had much luck with the length or char_length functions on > postgres. The length functions only work with strings. using them on numbers is usually the wrong thing as there is not a 1 to 1 mapping between strings an numbers. Strings of length only 3 can be out of range for integer (eg: '9e9'), (but numerics never look like that, larger floats can though) care to guess the result of this query? select '9000000000000000'::float, length('9000000000000000'::float::text); -- ⚂⚃ 100% natural