Monday, December 13, 2010

Usage of ISNUMERIC and how to create ISALLDIGITS functionality

/* Usage of ISNUMERIC */

/*
checks whether a number is Digits, Decimal, floating point, number, money
*/

//
A point of summary

 SELECT [Ascii Code]        = STR(Number),
[Ascii Character]   = CHAR(Number),
[ISNUMERIC Returns] = ISNUMERIC(CHAR(Number))
FROM Master.dbo.spt_ValuesWHERE Type = 'P'
AND Number BETWEEN 0 AND 255
AND ISNUMERIC(CHAR(Number)) = 1

//Results

SELECT ISNUMERIC(',') // returns 1 since the decimal contains ','
SELECT ISNUMERIC('$') // returns 1 since the money contain '$'
SELECT ISNUMERIC('€') // returns 1 since the money contain '€'
SELECT ISNUMERIC('£') // returns 1 since the money contain '£'
SELECT ISNUMERIC('¥') // returns 1 since the money contain '¥'

// so ISNUMERIC is not a permanent solution for checking ISALLDIGITS
// ISALLDIGITS Snippet

CREATE FUNCTION dbo.IsAllDigits/********************************************************************
Purpose:
This function will return a 1 if the string parameter contains only
numeric digits and will return a 0 in all other cases.  Use it in
a FROM clause along with CROSS APPLY when used against a table.

--Siva Shunmugam
********************************************************************/
(@MyString VARCHAR(8000))
RETURNS TABLE AS
RETURN (
SELECT CASE
WHEN @MyString NOT LIKE '%[^0-9]%'
THEN 1
ELSE 0
END AS IsAllDigits




No comments:

Post a Comment