IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New OK, here's the "foolproof-but-fugly" solution:
Like I said, based on SubStr() and Decode():

Select
  Decode(SubStr(TextField, 1, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 2, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 3, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 4, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 5, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 6, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 7, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 8, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) ||
  Decode(SubStr(TextField, 9, 1), '0', '0', '1', '1', '2', '2', '3', '3',
    '4', '4', '5', '5', '6', '6', '7', '7', '8', '8', '9', '9', NULL) Num_Text
From Table;


As you can see, this kind of does the same thing as Translate() -- translate '1' to '1', '2' to '2', etc -- only, the "Otherwise" parameter(*) to Decode() guarantees that everything else gets translated into nothing at all. The key "magic" is the fact (? I certainly hope so!) that concatenating a string with NULL yields, not NULL, but the original string. The downside is, obviously, that we have to do it one character at a time. But, on the gripping hand: It works.

Now, this is based (and tested) on a nine-character "TextField"; if yours is longer, you just duplicate and adapt the long butt-ugly line(#) for each extra character; increment the second parameter to SubStr() (the bold red digits above) and keep concatenating each potentially-numeric result character to your total result. If yours is shorter, you delete a few lines from the end of the above, of course -- just remember to remove the "||" concatenation operator from the last one. The result of this operation will still be a string variable (i.e, of type VarChar2, presumably), albeit containing only numeric characters; if you want it as a genuine numeric one, then wrap the whole shebang in a call to the To_Num() function.

[Edit - forgot to add:] What I didn't test, is performance. If you recall the thread here about Decode() a little while ago, opinion about its use was divided; it can have a significant impact on performance sometimes; in other situations, it's negligible. Nothing for it but to try it out, I suspect... Oh, what the heck, I'll try it out forya! OK, results: Selecting, as per the above, the numerics of a VarChar2(10) field from a 208,000-row table took 4.1 seconds. This field, although defined as character, seems to contain only numbers. (I wrapped it in Max() and Min(), so as to eliminate 208,000 rows using up our bandwidth and scrolling up my screen; same reult both times.) Tried another field, VarChar2(30) (this one doesn't seem to contain any numerics at all), the same way; that took 15 seconds.

HTH!




(*): Very useful, that "default" parameter -- the rule-of-thumb to remember is, if the number of parameters to Decode() is even, then every source value not specified in the previous parameters stays as-is; if the number of parameters is odd, then every non-specified value gets translated into the last, "odd-man-out", parameter value.

(#): Each "conceptual" line is split into two "actual" lines above, so as to stave off horizontal scrolling in your browser; the "line" that you are supposed to duplicate is the text from "Decode(SubStr(" to "'9', NULL) ||".


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
Expand Edited by CRConrad May 7, 2004, 04:14:18 AM EDT
New Believe me I came close to writing something similar
That's tops!

It's a 20 charater field, so it would be managable.

I'd have to test the 'concatinating Null to something still gives you something' though. You can't do TRANSLATE(NumField,'ABCDetc...','') because anything evaluated with a Null makes the whole thing Null (Oracle have a better explanation than I!)

Even if NULL isn't allowed, it could be DECODEd to something, (eg: 'Z') and then a further TRANSLATE could get rid of the Z. So there you go - I could take this code from fugly to ... fuglier.

Anyway, it goes like this: the requestor of this field knows the issue, knows it's not foolproof, and is likely to sign off on it anyway. It still bugs me that the solution isn't perfect, and I am reasonable sure the requestor would not claim ownership of the SUBSTR-each-character SQL, technically perfect though it is.

Whatever. It's Friday evening. I'm home now. What am I doing talking about work?!

/me cracks open a beer...

Edit: Typo in subject line. I blame the beer...
Two out of three people wonder where the other one is.
Expand Edited by Meerkat May 7, 2004, 04:16:30 AM EDT
New *shudder*
--
[link|mailto:greg@gregfolkert.net|greg],
[link|http://www.iwethey.org/ed_curry|REMEMBER ED CURRY!] @ iwethey

Give a man a match, he'll be warm for a minute.
Set him on fire, he'll be warm for the rest of his life!
     Oracle: An inverse of the TRANSLATE function? - (Meerkat) - (10)
         No, seems to be exactly what Oracle recommends for that task - (CRConrad) - (2)
             Wrong attitude - (ben_tilly) - (1)
                 Trouble is, the process doesn't crash, - (Meerkat)
         OK, here's the "foolproof-but-fugly" solution: - (CRConrad) - (2)
             Believe me I came close to writing something similar - (Meerkat)
             *shudder* -NT - (folkert)
         What version of Oracle? - (broomberg) - (3)
             That's cool - (ben_tilly)
             Gah. - (admin)
             Well spotted! - (Meerkat)

Hey, you sass that hoopy Ford Prefect?
62 ms