Re: Testing castability of text to numeric - Mailing list pgsql-sql

From Dean Gibson (DB Administrator)
Subject Re: Testing castability of text to numeric
Date
Msg-id 5.1.0.14.2.20030519171925.00ad4db0@imaps.ultimeth.net
Whole thread Raw
In response to Re: Testing castability of text to numeric  (Christoph Haller <ch@rodos.fzk.de>)
List pgsql-sql
Try something like this:<br /><br /><tt>numeric_column = CASE<br />   WHEN trim( text_column, '0123456789' ) = ''
THEN<br/>     text_column::NUMERIC<br />   ELSE<br />     NULL<br />   END<br /><br /></tt>If your text_column also has
blanks(leading or trailing), you may want to adapt the above slightly, but I think you get the idea.<br /><br /> I
don'tknow how to do arbitrarily formatted dates.<br /><br /> -- Dean<br /><br /> Achilleus Mantzios wrote on 2003-05-16
07:57:<br/><blockquote cite="cite" class="cite" type="cite">On Fri, 16 May 2003, Christoph Haller wrote:<br /><br />
>><br /> > > I am trying to create a function that will test if a given text value<br /> > > may
safelybe cast to numeric, returning the numeric cast or null if<br /> > > impossible.<br /><br /> I think that
progressis being made on<br /> having java as a serverside language<br /> (i.e. plpgjava).<br /> Stored procedures in
javawould be really handy in order<br /> to do staff like that, java is known for its<br /> "exceptional" exception
handling!<br/><br /> What language do you use to bould your applications??<br /><br /> > ><br /> > > Is
therea way to "catch" the "ERROR:  Bad numeric input format"<br /> > error?<br /> > ><br /> > > I ask
thatrather than about what the numeric format is for<br /> > regex-style<br /> > > testing because if this
worksI may want to use a similar method to<br /> > > check if a given text value casts safely to other types,
likedate.<br /> > ><br /> > I don't know of any way to catch errors.<br /> > And I can't see any way how to
testcastability via regex. Can you?<br /> > The only way I can think of at the moment is writing a C function.<br />
><br /> > Regards, Christoph<br /> > <br /> > <br /> > <br /> > ---------------------------(end of
broadcast)---------------------------<br/> > TIP 4: Don't 'kill -9' the postmaster<br /> > <br /><br /> -- <br />
==================================================================<br/> Achilleus Mantzios<br /> S/W Engineer<br /> IT
dept<br/> Dynacom Tankers Mngmt<br /> Nikis 4, Glyfada<br /> Athens 16610<br /> Greece<br /> tel:    +30-210-8981112<br
/>fax:    +30-210-8981877<br /> email:  achill@matrix.gatewaynet.com<br />         mantzios@softlab.ece.ntua.gr<br
/><br/><br /> ---------------------------(end of broadcast)---------------------------<br /> TIP 4: Don't 'kill -9' the
postmaster</blockquote>

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: CASE
Next
From: Jan Wieck
Date:
Subject: Re: "deadlock detected" / cascading locks