Important alert: (current site time 7/15/2013 7:50:51 PM EDT)
 

VB icon

Check only for numeric digits without using a loop

Email
Submitted on: 7/29/2000 9:55:41 PM
By: Umachandar  
Level: Intermediate
User Rating: By 2 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 13549
 
     The ISNUMERIC function in SQL60/65/70 checks for decimal & integer values. Hence characters like D, E are valid float representations & similarly ','. This is a simple logic that can check only for numeric digits without using a loop of any kind
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Check only for numeric digits without using a loop
-- Description:The ISNUMERIC function in SQL60/65/70 checks for decimal & integer values. Hence characters like D, E are valid float representations & similarly ','. This is a simple logic that can check only for numeric digits without using a loop of any kind
-- By: Umachandar
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=195&lngWId=5--for details.--**************************************

-- The numbers are hard-coded in the SELECT below but
-- use a NUMBERS table instead. This table can also be
-- used to solve several other problems quite easily.
declare @c varchar(10)
select @c = '12' + char(9)
if exists(select * from
		(select 1 as digit union all select 2 union all
			select 3 union all select 4 union all
			select 5 union all select 6 union all
			select 7 union all select 8 union all
			select 9 union all select 10) as n
		where ascii(substring(@c, digit, 1 )) not between 48 and 57 )
	print 'No'
else
	print 'Yes'
-- Or to check for unsigned integers alone
if patindex( '%[^0-9]%' , @c ) > 0
	print 'No'
else
	print 'Yes'


Other 133 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/23/2000 2:53:22 PMRobert

Nice and tight..........
Way to go
(If this comment was disrespectful, please report it.)

 
6/4/2002 5:31:21 PMnickdanger

i get no everytime no matter what i set @c. Should the print statements be reversed? if you are looking for numerics then shouldn't the "NOT" be removed from BETWEEN 48 and 57?
(If this comment was disrespectful, please report it.)

 
11/16/2006 10:14:29 PMJeff Moden

Nicely done and I agree about the numbers/tally table. If it's for a pure integer value, this doesn't require either and is short enough to not need to be in a function... the following will return those rows that only have numeric digits 0-9 in the column called "somecolumn"....

SELECT *
FROM sometable
WHERE somecolumn NOT LIKE '%[^0-9]%'


(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.