473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

isdigit ?

How can I check if a char/string is a digit/number in an SQL UDF?
Dec 20 '07 #1
7 5995
On Dec 20, 10:17 pm, E.Bartosiew...@gmail.com wrote:
How can I check if a char/string is a digit/number in an SQL UDF?
If all characters are digit,
TRANSLATE(char_str,'*',' 0123456789') = ''
If leading/trailing blanks are allowed,
TRANSLATE(LTRIM(RTRIM(char_str)),'*',' 0123456789') = ''
Dec 20 '07 #2
Tonkuma wrote:
On Dec 20, 10:17 pm, E.Bartosiew...@gmail.com wrote:
>How can I check if a char/string is a digit/number in an SQL UDF?
If all characters are digit,
TRANSLATE(char_str,'*',' 0123456789') = ''
If leading/trailing blanks are allowed,
TRANSLATE(LTRIM(RTRIM(char_str)),'*',' 0123456789') = ''
Another alternative is to use the soft_int function that Serge Rielau posted
here a while ago. This function would return the number as INT (or NULL if
the string doesn't contain a valid number).

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Dec 20 '07 #3
On Thu, 20 Dec 2007 18:27:17 +0100, Knut Stolze wrote:
Another alternative is to use the soft_int function that Serge Rielau
posted here a while ago. This function would return the number as INT
(or NULL if the string doesn't contain a valid number).
Make that "softint".

However, in this case, I wouldn't be surprised if the string analysis
method proposed by Tonkuma is actually faster than the "try casting
through a function which calls a procedure" method which is used by
Serge's softint solution.

--
Regards,
Troels Arvin <tr****@arvin.dk>
http://troels.arvin.dk/
Dec 20 '07 #4
Troels Arvin wrote:
On Thu, 20 Dec 2007 18:27:17 +0100, Knut Stolze wrote:
>Another alternative is to use the soft_int function that Serge Rielau
posted here a while ago. This function would return the number as INT
(or NULL if the string doesn't contain a valid number).

Make that "softint".

However, in this case, I wouldn't be surprised if the string analysis
method proposed by Tonkuma is actually faster than the "try casting
through a function which calls a procedure" method which is used by
Serge's softint solution.
No question about that. It really depends on what the OP wants to do. If
it is something like:

CASE
WHEN isDigit(str)
THEN INT(str)
END

Then Serge's function may be a perfect match. If it is really only a test,
then I'd go with Tonkuma's approach, of course.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Dec 21 '07 #5
>How can I check if a char/string is a digit/number in an SQL UDF? <<

Wouldn't it be better to add a " CHECK( foobar IS SIMILAR TO
' [:DIGITS:]+') to the column and prevent the bad data?

Dec 28 '07 #6
Although, DB2 doesn't support "SIMILAR TO" syntax,
Knut showed very general solution for regex(s) UDF(s) in
"Bringing the Power of Regular Expression Matching to SQL"
http://www.ibm.com/developerworks/db...301stolze.html

Another more poor and simple way may be ....
CREATE TABLE T_check_isdigit
(key INTEGER NOT NULL PRIMARY KEY
,char_str VARCHAR(10) NOT NULL
CHECK( TRANSLATE(char_str,'*',' 0123456789') = '' )
);
Dec 29 '07 #7
>Although, DB2 doesn't support "SIMILAR TO" syntax, .. <<

Sorry -- I write ANSI/USO Standard SQL automatically.
Dec 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James Gregory | last post by:
I read that the argument to isdigit() can be "an integer whose value is representable as an unsigned char, or the value of the macro EOF.". This seems to say that it should work for values greater...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
7
by: Per Rollvang | last post by:
Hi All! I have a problem with hex strings when using Char.IsDigit(). The hex strings(i.e. a CRC32 checksum) get a false result for letters A-F... MSDN tell me that: Indicates whether a...
2
by: Jefe | last post by:
Hi Group Could you please explain to me what's the difference between Chars.IsDigit and Chars.IsNumber? Regards,
10
by: mdh | last post by:
Could I get some help as to understanding why I am not getting a result I expect. #include <stdio.h> #include <ctype.h> int main (){ int i,j,k,c; i=9;
16
by: skneife | last post by:
Doeas any know the simplest way for create this type of function: bool result = IsDigit(string s); Sam
3
by: MooMaster | last post by:
N00b question alert! I did a search for isdigit() in the group discussion, and it didn't look like the question had been asked in the first 2 pages, so sorry if it was... The manual...
2
by: bozo789 | last post by:
Hi Forum, First let me begin by stating I am a student and have read the proper posting procedure concerning students. I am a begginer learning with the Miracle C Work Bench. I know my code is...
1
by: khaichiew85 | last post by:
im having a program.I want it to check if it is a character or integer is entered where shall i put the isDigit and how to use?i put it as highlighted below but got error. #include<stdio.h>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.