Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Using PATINDEX to find all numeric values Expand / Collapse
Author
Message
Posted Thursday, February 14, 2008 3:07 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, March 27, 2013 3:29 PM
Points: 475, Visits: 273
Hello,

I forget. How can PATINDEX be used to find column values where the data is all numeric? I've tried, the following:

select distinct acct from tbl_CYProcessedSales
where PatIndex('%[0-9]%',Acct) > 0
order by acct

Acct is varchar(8). What am I doing wrong?

Thank you for your help!
CSDunn



Post #456023
Posted Thursday, February 14, 2008 3:21 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 2:18 PM
Points: 2,278, Visits: 2,999
I would use ISNUMERIC

select distinct acct 
from tbl_CYProcessedSales
where ISNUMERIC(Acct) > 0
order by acct





My blog: http://jahaines.blogspot.com
Post #456031
Posted Saturday, February 16, 2008 11:10 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 32,930, Visits: 26,816
Oh... be careful, now... IsNumeric cannot be equated to IsAllDigits. IsNumeric will allow currency symbols, $, comma, decimal point, spaces, tabs, and a host of other individual characters. I will also allow for combinations of charcters...

SELECT ISNUMERIC('2d2'), ISNUMERIC('2e2')

In fact, I wrote a pretty hefty article about this on another web site...

ISNUMERIC is not “ALL DIGITS”Submitted by Jeff Moden, 03 Jun 2006
All rights reserved.

Introduction:

There are many cases where you need to ensure that the string data you are working with includes only numeric digits. Most Developers will use the built in ISNUMERIC function to make such a check. Here’s why that’s a bad idea and what to do about it.

What is ISNUMERIC?

“Books OnLine” summarizes the description of the ISNUMERIC function as:

“Determines whether an expression is a valid numeric type.”

and that’s a 100% accurate description that leaves much to be desired. Just what is a “valid numeric type”? Reading further in BOL (short for “Books OnLine), we find additional information:

“ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 guarantees that expression can be converted to one of these numeric types.”

Again, read the wording… “when the input expression evaluates to a valid integer”, etc, etc. And, that’s the catch. There are many different things that you may not expect that will evaluate to one of the data types listed in the description of ISNUMERIC and a lot of them are NOT the digits 0-9. ISNUMERIC will return a “1” for all of them.

Let’s consider the most obvious… what will ISNUMERIC(‘-10’) return? What will ISNUMERIC(‘1,000’) return? And how about the not-so-obvious… what will ISNUMERIC('0d1234') or ISNUMERIC('13e20') return? There are many different combinations of letters, numbers, and symbols that can actually be converted to numeric data types and ISNUMERIC will return a “1” for all of them. It’s not a flaw… that’s the way it’s supposed to work!


What IS Actually Considered “Numeric” by ISNUMERIC?

This code will show all of the single characters that ISNUMERIC thinks of as “Numeric”…

--===== Return all characters that ISNUMERIC thinks is numeric
-- (uses values 0-255 from the undocumented spt_Values table
-- instead of a loop from 0-255)
SELECT [Ascii Code] = STR(Number),
[Ascii Character] = CHAR(Number),
[ISNUMERIC Returns] = ISNUMERIC(CHAR(Number))
FROM Master.dbo.spt_Values
WHERE Name IS NULL
AND ISNUMERIC(CHAR(Number)) = 1

That code produces the following list of characters…

Ascii Code Ascii Character ISNUMERIC Returns 
---------- --------------- -----------------
9 1
10
1
11
1
12 1
13
1
36 $ 1
43 + 1
44 , 1
45 - 1
46 . 1
48 0 1
49 1 1
50 2 1
51 3 1
52 4 1
53 5 1
54 6 1
55 7 1
56 8 1
57 9 1
128 € 1
160 1
163 £ 1
164 ¤ 1
165 ¥ 1

What are these characters?

Ascii 9 is a TAB character and is included because a column of numbers is frequently delimited by a TAB.

Ascii 10 is a Line Feed character and is included because the last column of numbers is frequently terminated by a Line Feed character.

Ascii 11 is a Vertical Tab character and is included because the last column of numbers is frequently terminated by a Vertical Tab character.

Ascii 12 is a Form Feed character and is included because the last column numbers of the last row is sometimes terminated by a Form Feed character.

Ascii 13 is a Carriage Return character and is included because the last column of numbers is frequently terminated by a Carriage Return character.

Ascii 36 (Dollar sign), 128 (Euro sign), 163 (British Pound sign), and 164 (Yen sign) are included because they are frequently used as enumerators to identify the type of number or, in this case, the currency type the number is meant to represent.

Ascii 43 (Plus sign), 44 (Comma), 45 (Minus sign), and 46 (Decimal place) are included because they are frequently included in numeric columns to mark where on the number line the number appears and for simple formatting.

Ascii 160 is a special "hard space" and is included because it is frequently used to left pad numeric columns so the column of numbers appears to be right justified.

Ascii 32 is a "soft space" and is not included because a single space does not usually represent a column of numbers. Ascii 32 is, however, a valid numeric character when used to create right justified numbers as is Ascii 160 but a single Ascii 32 character is NOT numeric. In fact, a string of Ascii 32 spaces is not considered to be numeric but a string of spaces with even a single digit in it is considered to be numeric.

Ascii 164 is a special character and is included because it is frequently used by accountants and some software to indicate a total or subtotal of some type. It is also used by some to indicate they don't know what the enumerator is.

Ascii 48-59 are included because they represent the digits 0 through 9

Set of Characters Treated as “Numeric” by ISNUMERIC

Do notice that "e" and "d" (everybody forgets about this) are not included as numeric in the results because a single "e" or "d is NOT considered to be numeric. HOWEVER, these letters are for two different forms of scientific notation. So, if you have anything that looks like the following, ISNUMERIC will identify them as “Numeric”…

SELECT ISNUMERIC('0d2345')
SELECT ISNUMERIC('12e34')

The “Rational” Solution

Hopefully, I’ve proven that ISNUMERIC is NOT the way to determine if a value or a column of values IS ALL DIGITS. So, what to do? We could write something really complex that loops through each character to see if it’s a digit… or … we can use a very simple rational expression to do the dirty work for us. The formula is…

NOT LIKE '%[^0-9]%'

… and it can be used directly (preferred method for performance reasons)…

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

… or, if you don’t mind the performance hit, you can create your own “IsAllDigits” function…

 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.

--Jeff Moden
********************************************************************/
--===== Declare the I/O parameters
(@MyString VARCHAR(8000))
RETURNS INT
AS
BEGIN
RETURN (SELECT CASE
WHEN @MyString NOT LIKE '%[^0-9]%'
THEN 1
ELSE 0
END)
END

If you have any questions on this article, please don’t hesitate to post them. And thanks for taking the time to read it and, perhaps, get your favorable vote .


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #456691
Posted Sunday, February 17, 2008 12:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 2:18 PM
Points: 2,278, Visits: 2,999
Good article Jeff.

I guess the method of choice comes down to knowing your data. If his column is what I think it is, an integer column converted to a varchar; he should be alright, but you never know.




My blog: http://jahaines.blogspot.com
Post #456692
Posted Monday, February 18, 2008 8:44 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, May 17, 2013 7:43 AM
Points: 808, Visits: 1,600
In fact, I wrote a pretty hefty article about this on another web site...

ISNUMERIC is not “ALL DIGITS”Submitted by Jeff Moden, 03 Jun 2006
All rights reserved.

Thanks Jeff. Great article and very understandable, even for those of us who are NOT DBA's.

Julie
Post #456925
Posted Monday, February 18, 2008 9:14 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 32,930, Visits: 26,816
Thanks for the feedback, Julie... hmm... maybe I should publish it here, as well.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #456943
Posted Thursday, July 31, 2008 10:14 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 14, 2013 2:15 PM
Points: 2, Visits: 166
Of course this isnt a perfect solution:

What you would really like to isolate are values that won't cast as float like '11,22,33' or 1234e456', etc.

Your new function returns a value of "0" for values like '1,000,000' or '1 e -23' which will actuall cast as float just fine. Which are valid numeric values.

Now if somebody can find an efficient way of doing that short of opening a cursor and cast each value that would be usefull . . .
Post #544538
Posted Thursday, July 31, 2008 5:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:06 PM
Points: 32,930, Visits: 26,816
Heh... of course it's a perfect solution... but only for what it was designed for... it's an IsAllDigits function and wasn't meant to be anything else. If you want to be able to detect anything that will convert to Float or Money, then IsNumeric works just fine. ;)

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #544786
Posted Thursday, July 31, 2008 11:50 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 14, 2013 2:15 PM
Points: 2, Visits: 166
No IsNumeric('11,22,33') = 1 but cast ('11,22,33' as float) will raise an error. So isnumeric is not the perfect way to select data that will cast as float, money, or numeric, etc. from a char datatype and all its cousins.

You do have a point about working as designed. I guess I just don't see exactly how it could be useful if it's not a better mouse trap. Say hypothetically I needed a way to eliminate all char type values from a field before I casted them as float. This query:

Select Cast(columnA as float) From Table1 Where Isnumeric(ColumnA)

falls short because of the issue I raised above. I can still raise an error converting certain char values that pass the isnumeric sniff test but will not actually cast as float. If I use your function I get the opposite:

Select Cast(columnA float) From Table1 Where IsAllDigits(ColumnA)

Here I run the risk of omitting values that could safely cast as float. So in the end, IsAllDigits is fine as far as working as designed. It just may be as useful as it seems.

Nice write up though, don't get me wrong . . . =)
Post #544859
Posted Friday, August 01, 2008 2:45 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, March 01, 2012 2:02 AM
Points: 228, Visits: 209
cdun2 (2/14/2008)
Hello,

I forget. How can PATINDEX be used to find column values where the data is all numeric? I've tried, the following:

select distinct acct from tbl_CYProcessedSales
where PatIndex('%[0-9]%',Acct) > 0
order by acct

Acct is varchar(8). What am I doing wrong?

Thank you for your help!
CSDunn


As mentioned, u want to find all the columns where data is all NUMERIC.... so will it include special symbols???
Anywayz, If u have to use PATINDEX, refer below query:

This Query exclude the data having special symbols.

SELECT DISTINCT acct
FROM tbl_CYProcessedSales
WHERE patindex('%[~`!@#$%^&*()_=+\|{};",<>/?a-z]%',acct)=0
ORDER BY acct

I hope this is what you want...

--Samarth :)
Post #544912
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse