Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 99712 invoked from network); 12 Mar 2010 08:52:39 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 12 Mar 2010 08:52:39 -0000 Received: (qmail 85818 invoked by uid 500); 12 Mar 2010 08:52:03 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 85640 invoked by uid 500); 12 Mar 2010 08:52:03 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 85633 invoked by uid 99); 12 Mar 2010 08:52:02 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Mar 2010 08:52:02 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of victorianladyturtle@gmail.com designates 209.85.216.203 as permitted sender) Received: from [209.85.216.203] (HELO mail-px0-f203.google.com) (209.85.216.203) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Mar 2010 08:51:55 +0000 Received: by pxi41 with SMTP id 41so379416pxi.23 for ; Fri, 12 Mar 2010 00:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:return-receipt-to:reply-to :from:to:references:in-reply-to:subject:date:organization:message-id :mime-version:content-type:content-transfer-encoding:x-mailer :thread-index:content-language:disposition-notification-to; bh=QDA1IhMt3utPdJfK1XvRu5GGLQaJfh/n2vQKFolOOQ0=; b=gN1J5f0uAi+mymznVORl3w5+mBd3y0eQsVtj2ozq62t3J25tt7Qbcgi6Oli+hjELHa Us2wHDSp05GeANjWm5p9DSwoJRN4sjsBqXCSoz6TLKo1xP/xiCYikCeQYctkdXen5vyL MMUTUvp9/pzGfQcnt0mXeswzSPIBE6ANseQhI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=return-receipt-to:reply-to:from:to:references:in-reply-to:subject :date:organization:message-id:mime-version:content-type :content-transfer-encoding:x-mailer:thread-index:content-language :disposition-notification-to; b=cJUZCXBbEmzFMsrxq1axNnJ5r7wP0HqnNtsdNwTLfTbX3BLX6D3aarRfbv3tOPwiCt IQBVhcddXV2xaE+qOtlIFNXkIof43MXm5Hqps/IIA0CIAMTiujLNJq+sGxi47VqxJDgy 9oxMz03rec/jNizCXpwTNF8/pxC8FLagcpbp8= Received: by 10.142.151.18 with SMTP id y18mr2238702wfd.338.1268383893600; Fri, 12 Mar 2010 00:51:33 -0800 (PST) Received: from NettaToshiba (adsl-99-70-127-178.dsl.lsan03.sbcglobal.net [99.70.127.178]) by mx.google.com with ESMTPS id 34sm322091yxf.18.2010.03.12.00.51.31 (version=SSLv3 cipher=RC4-MD5); Fri, 12 Mar 2010 00:51:32 -0800 (PST) Reply-To: From: "Annetta C Green" To: "'Derby Discussion'" References: <58C9501833EA44618C0651C5FA9FF878@atlantis> <4B97D370.5030401@sun.com> <49EF399C79174BF099417BFABBCB929B@atlantis> <4B98F41F.9090605@sun.com> <4B98F7EE.6030805@chicoree.fr> In-Reply-To: <4B98F7EE.6030805@chicoree.fr> Subject: RE: Converting a value held in a VARCHAR column into an INTEGER Date: Fri, 12 Mar 2010 00:51:27 -0800 Organization: NettaSoft Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcrBSNugUKupoidnS02YAapL8np3KAAdVsYg Content-Language: en-us X-Virus-Checked: Checked by ClamAV on apache.org Hi, Since everything is being brought in as a VARCHAR, is there any reason = you cannot use the .startsWith(") method from the String class? public static int convertToInt(String str) { int myInteger =3D 0; for (int x =3D 0; x < 10; x++)=20 { if(str.startsWith(String.valueOf(x)) { myInteger =3D Integer.parseInt(str); break; }//end if }//end for return myInteger; }//end convertToInt Respectfully, Annetta C. Green Senior Developer NettaSoft Email: annetta@nettasoftonline.com Website: http://www.nettasoftonline.com -----Original Message----- From: Sylvain Leroux [mailto:sylvain@chicoree.fr]=20 Sent: Thursday, March 11, 2010 6:02 AM To: Derby Discussion Subject: Re: Converting a value held in a VARCHAR column into an INTEGER Hi again Jazz, Hi Rick, Since there was at least one typo in my previous message (I forgot the "public"=20 modifier for the static method), here is a complete transcription for = the=20 function-based solution: # # Java source code: # sh$ cat MyFunctions.java public class MyFunctions { public static Integer toInt(String str) { try { return Integer.valueOf(str); } catch(NumberFormatException nfe) { return null; } } } sh$ javac MyFunctions.java # # Demo in ij: # sh$ export CLASSPATH=3D.:$DERBY_HOME/lib/derby.jar:$DERBY_HOME/lib/derbytools.jar sh$ java org.apache.derby.tools.ij ij version 10.5 ij> CONNECT 'jdbc:derby:dummy;create=3Dtrue'; ij> CREATE TABLE TBL(value VARCHAR(255)); ij> INSERT INTO TBL(value) VALUES('1'), ('7'), ('*'), ('10'), ('{'), = ('99'); 6 rows inserted/updated/deleted ij> CREATE FUNCTION toInt(str varchar(255)) RETURNS int PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL NAME 'MyFunctions.toInt'; 0 rows inserted/updated/deleted ij> SELECT toInt(value) FROM TBL; 1 ----------- 1 7 NULL 10 NULL 99 6 rows selected ij> SELECT value FROM tbl WHERE toInt(value)>7; VALUE=20 -------------- 10 99 2 rows selected ij> Rick Hillegas a =E9crit : >=20 > Hi Jazz, >=20 > Some more comments inline... >=20 > Jasvinder S. Bahra wrote: >>> You may be able to use a CASE expression to solve this problem: >>> >>> SELECT name, power FROM card >>> WHERE >>> ( case when power =3D '*' or power =3D '{' or power =3D '^' then = null else=20 >>> integer( power ) end ) > 7; >> >> Unfortunately, the non-numeric characters I mentioned are the only=20 >> ones in the data set *at the moment*. In the future, other=20 >> non-numeric characters may be introduced - which makes this technique = >> inpractical. > Another solution would be to write a function which returns null for=20 > your special characters and returns an integer for values which really = > are numbers. As you add more non-numeric characters, you just have to=20 > adjust this function. Then your query would look like this: >=20 > select name, power from card > where myCastFunction( power ) > 7; >> >> I think i'm going to have to bite the bullet and just store the data=20 >> in two columns - one of type INTEGER, the other VARCHAR, and just = make=20 >> sure the INSERT logic only populates the INTEGER column if the value=20 >> is made up of numeric characters. > If you pursue this approach, you are still going to have to maintain = the=20 > triaging logic which separates numbers from non-numeric strings. You=20 > could put the triaging logic in a function which is invoked at INSERT=20 > time and then add a generated column to your table: >=20 > CREATE TABLE card ( > name VARCHAR(64) NOT NULL, > power VARCHAR(16) NOT NULL, > integerPower int generated always as ( myCastFunction( power ) ) > ); >=20 > The advantage of this approach is that you can put a useful index on=20 > integerPower and that might speed up your queries. >=20 > Hope this helps, > -Rick >> >> I know this is a bit of a no-no in terms of database design, but I=20 >> can't see any way around it. >> >> In any case, thanks for the suggestion Rick. >> >> Jazz >> >> >=20 >=20 >=20 --=20 sylvain@chicoree.fr http://www.chicoree.fr