
November 15th, 2005, 11:25 PM
| | | How test for all digits
I can see no function which tests its argument to be all digits and I
see no easy way to compose such a function from translate, replace, etc.
I must be missing something simple, right? | 
November 16th, 2005, 07:55 AM
| | | Re: How test for all digits
Bob Stearns wrote:
[color=blue]
> I can see no function which tests its argument to be all digits and I
> see no easy way to compose such a function from translate, replace, etc.
> I must be missing something simple, right?[/color]
Something like this should do the trick:
CASE
WHEN LENGTH(REPLACE(REPLACE(...(REPLACE(str, '0', ''), '1', ''), ...),
'9', '')) > 0
THEN 0
ELSE 1
END
Or you use a loop, SUBSTR and BETWEEN...
--
Knut Stolze
DB2 Information Integration Development
IBM Germany | 
November 16th, 2005, 01:55 PM
| | | Re: How test for all digits
CASE
WHEN LENGTH(RTRIM(TRANSLATE(test_str, '', '0123456789'))) = 0 THEN
'All digits'
ELSE 'No'
END | 
November 16th, 2005, 03:25 PM
| | | Re: How test for all digits
If you are feeling particularly evil, you could just INTEGER() the
argument and trap error 420.
B. | 
November 16th, 2005, 07:15 PM
| | | Re: How test for all digits
Knut Stolze wrote:
[color=blue]
> Bob Stearns wrote:
>
>[color=green]
>>I can see no function which tests its argument to be all digits and I
>>see no easy way to compose such a function from translate, replace, etc.
>>I must be missing something simple, right?[/color]
>
>
> Something like this should do the trick:
>
> CASE
> WHEN LENGTH(REPLACE(REPLACE(...(REPLACE(str, '0', ''), '1', ''), ...),
> '9', '')) > 0
> THEN 0
> ELSE 1
> END
>
> Or you use a loop, SUBSTR and BETWEEN...
>
>[/color]
How slow is this going to be as a CHECK constraint? | 
November 16th, 2005, 07:15 PM
| | | Re: How test for all digits
Tonkuma wrote:
[color=blue]
> CASE
> WHEN LENGTH(RTRIM(TRANSLATE(test_str, '', '0123456789'))) = 0 THEN
> 'All digits'
> ELSE 'No'
> END
>[/color]
It was not clear from the docs what would happen with an empty string as
the second argument to TRANSLATE. Consider the following passage from
SQL Reference Vol 1:
pad-char-exp
Is a single character that will be used to pad the to-string-exp if the
to-string-exp is shorter than the from-string-exp. The pad-char-exp must
have a length attribute of one, or an error is returned. If not present,
it will be taken to be a single-byte blank.
My interpretation of that is that an empty string as second argument
will be treated as a string of blanks as long as the third argument, so
the example would never work. | 
November 16th, 2005, 08:15 PM
| | | Re: How test for all digits
An obvious note is, there is a built-in CONSTRAINT to make sure that
data is numeric, it's the data type of INTEGER (or its ilk). What is a
data type other than a CONSTRAINT?
Also, if the data is guaranteed to be alphanumeric, SOUNDEX() can be
used. Being SOUNDEX() will always return the first letter for the first
character (in the four character response) and Z if no alphabetic
characters exist, two checks can be made.
CHECK(SOUNDEX(Text) = 'Z000' AND SOUNDEX(Text || 'A') = 'A000')
B. | 
November 17th, 2005, 04:05 PM
| | | Re: How test for all digits
I did one mistake. If string includ blanks, they will be interpreted
digits in my former example.
Here is my corrected version
------------------------- Commands Entered -------------------------
SELECT test_str
, CASE
WHEN LENGTH(RTRIM(TRANSLATE(test_str, '*', ' 0123456789')))
= 0 THEN
'All digits'
ELSE 'No'
END
FROM TABLE(VALUES '9876012345'
, '20051118'
, 'ABCDEFG'
, '2005-11-18'
, '12345 67890'
) Q (test_str);
--------------------------------------------------------------------
TEST_STR 2
----------- ----------
9876012345 All digits
20051118 All digits
ABCDEFG No
2005-11-18 No
12345 67890 No
5 record(s) selected. | 
November 17th, 2005, 09:45 PM
| | | Re: How test for all digits
Tonkuma wrote:[color=blue]
> I did one mistake. If string includ blanks, they will be interpreted
> digits in my former example.
> Here is my corrected version
> ------------------------- Commands Entered -------------------------
> SELECT test_str
> , CASE
> WHEN LENGTH(RTRIM(TRANSLATE(test_str, '*', ' 0123456789')))
> = 0 THEN
> 'All digits'
> ELSE 'No'
> END
> FROM TABLE(VALUES '9876012345'
> , '20051118'
> , 'ABCDEFG'
> , '2005-11-18'
> , '12345 67890'
> ) Q (test_str);
> --------------------------------------------------------------------
>
> TEST_STR 2
> ----------- ----------
> 9876012345 All digits
> 20051118 All digits
> ABCDEFG No
> 2005-11-18 No
> 12345 67890 No
>
> 5 record(s) selected.
>[/color]
Thank you. | 
November 17th, 2005, 11:25 PM
| | | Re: How test for all digits
Depending on when you want to do the checking, ie, if you absolutely
don't want anything other than an integer to be stored in the database,
and then not worry about what comes out, here's a solution in VB:
____________
Public Function IsInteger(strNumInAsText As String) As Boolean ' For
either Integer or Long; No "," or "."
' There is no builtin ISINT function in VB. ISNUMERIC accepts commas
and decimal points (periods), which may be misplaced.
Dim lThisLong As Long
Dim lTextLength As Long
Dim iTestDigit As Integer
Dim lThisInt As Long
On Error Resume Next
IsInteger = False ' Initialize.
If Not IsNumeric(strNumInAsText) Then GoTo PROC_EXIT ' First
screen.
' It's "Numeric," but is it a legal integer according to our
definition? IE, Nothing but numbers?
lTextLength = Len(strNumInAsText)
If lTextLength > 10 Then GoTo PROC_EXIT '
Longer than the longest legal LONG(2,147,483,647).
For lThisInt = 1 To lTextLength
iTestDigit = Asc(Mid(strNumInAsText, lThisLong, 1))
If iTestDigit < 48 Or iTestDigit > 57 Then GoTo PROC_EXIT
' "0" is ASCII Chr(48) and "9" is Chr(57).
Next lThisInt
IsInteger = True
PROC_EXIT:
Exit Function
End Function
______________
You can change LONG to BIGINT if appropriate.
Yes, this looks like overkill, but when quick reading is more important
than writing quickly, it's the best way! | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,338 network members.
|