June 27, 2013 at 8:06 am
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works. And I don't want to use anything I can't support. Would someone mind giving me a blow by blow explanation of what each "thing" is doing here? (And if the statement needs to be improved please do so. I've seen some use a ^ in the statement before).
June 27, 2013 at 8:14 am
...here's an example of another version I've seen: NOT LIKE '%[^0-9]%'
June 27, 2013 at 8:32 am
DataAnalyst011 (6/27/2013)
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works. And I don't want to use anything I can't support. Would someone mind giving me a blow by blow explanation of what each "thing" is doing here? (And if the statement needs to be improved please do so. I've seen some use a ^ in the statement before).
This is a regular expression. All it does is checks if the the string has any number in it (0,1,2,3,4,5,6,7,8,9).
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 27, 2013 at 8:38 am
DataAnalyst011 (6/27/2013)
...here's an example of another version I've seen: NOT LIKE '%[^0-9]%'
This is another regular expression. This basically is saying give me all the rows where the value is not like NOT a number. It is kind of a double negative. The first one you posted checks if there is a number anywhere in the string, this one makes sure that every single character is a number.
Here is a code example to show you what I mean.
declare @SearchVal varchar(10) = 'asdf1234asdf'
select 'yes'
where @SearchVal like '%[0-9]%'
select 'yes'
where @SearchVal NOT LIKE '%[^0-9]%'
set @SearchVal = 'asdf'
select 'yes'
where @SearchVal like '%[0-9]%'
select 'yes'
where @SearchVal NOT LIKE '%[^0-9]%'
set @SearchVal = '2345'
select 'yes'
where @SearchVal like '%[0-9]%'
select 'yes'
where @SearchVal NOT LIKE '%[^0-9]%'
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 27, 2013 at 10:51 am
Thank you so much! This is very helpful.
June 28, 2013 at 1:14 pm
This should be helpful too: MSDN match expression article.
-- Itzik Ben-Gan 2001
July 2, 2013 at 12:48 am
Sean Lange (6/27/2013)
DataAnalyst011 (6/27/2013)
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works. And I don't want to use anything I can't support. Would someone mind giving me a blow by blow explanation of what each "thing" is doing here? (And if the statement needs to be improved please do so. I've seen some use a ^ in the statement before).This is a regular expression. All it does is checks if the the string has any number in it (0,1,2,3,4,5,6,7,8,9).
Hmmm... Technically I'd say it is Microsoft SQL's (somewhat limited) proxy for a Regular Expression. And I don't think they refer to it as that. I believe BOL always refers to it as a pattern (http://msdn.microsoft.com/en-us/library/ms179859.aspx).
The RegEx for this case would be something like: [0-9]{1,30}$ (or ^[0-9]{1,30}$ depending on whether you're doing a positive or negative test).
{1,30} specifies the overall length of the allowable characters.
There are add-ins to SQL (e.g., via CLR or the SQL Sharp library[/url]) that would allow validation by RegEx.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
July 2, 2013 at 7:19 am
dwain.c (7/2/2013)
Sean Lange (6/27/2013)
DataAnalyst011 (6/27/2013)
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works. And I don't want to use anything I can't support. Would someone mind giving me a blow by blow explanation of what each "thing" is doing here? (And if the statement needs to be improved please do so. I've seen some use a ^ in the statement before).This is a regular expression. All it does is checks if the the string has any number in it (0,1,2,3,4,5,6,7,8,9).
Hmmm... Technically I'd say it is Microsoft SQL's (somewhat limited) proxy for a Regular Expression. And I don't think they refer to it as that. I believe BOL always refers to it as a pattern (http://msdn.microsoft.com/en-us/library/ms179859.aspx).
The RegEx for this case would be something like: [0-9]{1,30}$ (or ^[0-9]{1,30}$ depending on whether you're doing a positive or negative test).
{1,30} specifies the overall length of the allowable characters.
There are add-ins to SQL (e.g., via CLR or the SQL Sharp library[/url]) that would allow validation by RegEx.
True it is not technically a regular expression but...a regular expression checks patterns in strings. Conceptually it is pretty much the same thing. It just isn't quite as robust. It is sort of like a t-sql mini-RegEx. 😛
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 27, 2013 at 5:17 pm
DataAnalyst011 (6/27/2013)
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works....here's an example of another version I've seen: NOT LIKE '%[^0-9]%'
Hi there. First things first: please define "numeric" and what you are REALLY checking for. Are you looking for:
These are all different concepts.
So first you need to be clear on what you will accept as being a number and what is not valid. Then you have several options, namely:
Take care,
Solomon...
SQL# — https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
Sql Quantum Lift — https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap — https://SqlQuantumLeap.com/ ( blog )
Info sites — Collations • Module Signing • SQLCLR
July 27, 2013 at 6:12 pm
DataAnalyst011 (6/27/2013)
From time to time I need to check if a column is completely numeric (or usually, check for the row contain something other than numeric). I've read the ISNUMERIC has problems. I've used LIKE '%[0-9]%'? successfully, but even after reading around in several places I still don't understand how it works. And I don't want to use anything I can't support. Would someone mind giving me a blow by blow explanation of what each "thing" is doing here? (And if the statement needs to be improved please do so. I've seen some use a ^ in the statement before).
Based on the word "ISNUMERIC" and the description above, I believe you're looking for an "IsAllDigits" solution. The following article will help with that.
http://www.sqlservercentral.com/articles/IsNumeric/71512/
I also recommend that you lookup "LIKE" in Books Online (the "help" system that comes with SQL Server). There will also be a popup when you search for "LIKE" in Books Online with the title "Pattern Matching in Search Conditions". That would be very good to study, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 29, 2013 at 6:20 am
Solomon Rutzky (7/27/2013)
- 23847234872893475983479583749583749573945739 is all digits, is a valid number, but is NOT convertible to any SQL Server number types as it is larger than 38 digits
...
You can convert it to float (with lose of some precision ;-))
select cast('23847234872893475983479583749583749573945739' as float)
July 29, 2013 at 7:23 am
Eugene Elutin (7/29/2013)
Solomon Rutzky (7/27/2013)
- 23847234872893475983479583749583749573945739 is all digits, is a valid number, but is NOT convertible to any SQL Server number types as it is larger than 38 digits
...
You can convert it to float (with lose of some precision ;-))
select cast('23847234872893475983479583749583749573945739' as float)
Considering that FLOAT only has 15 digits of precision, it'll be a pretty big loss. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
July 29, 2013 at 8:18 am
Jeff Moden (7/29/2013)
Eugene Elutin (7/29/2013)
Solomon Rutzky (7/27/2013)
- 23847234872893475983479583749583749573945739 is all digits, is a valid number, but is NOT convertible to any SQL Server number types as it is larger than 38 digits
...
You can convert it to float (with lose of some precision ;-))
select cast('23847234872893475983479583749583749573945739' as float)
Considering that FLOAT only has 15 digits of precision, it'll be a pretty big loss. 🙂
I wouldn't call 1.32% a such "big loss" :hehe:
However you are right! It does depend! If this loss constitutes my interest in £ - I would probably die from heart-attack 😀
July 29, 2013 at 8:53 am
Eugene Elutin (7/29/2013)
Jeff Moden (7/29/2013)
Eugene Elutin (7/29/2013)
Solomon Rutzky (7/27/2013)
- 23847234872893475983479583749583749573945739 is all digits, is a valid number, but is NOT convertible to any SQL Server number types as it is larger than 38 digits
...
You can convert it to float (with lose of some precision ;-))
select cast('23847234872893475983479583749583749573945739' as float)
Considering that FLOAT only has 15 digits of precision, it'll be a pretty big loss. 🙂
I wouldn't call 1.32% a such "big loss" :hehe:
However you are right! It does depend! If this loss constitutes my interest in £ - I would probably die from heart-attack 😀
Heh... absolutely agreed but wasn't talking about the loss in "value" of the number. Was talking about the number of digits that would be lost when trying to determine if a long string could be checked for "IsAllDigits".
--Jeff Moden
Change is inevitable... Change for the better is not.
July 29, 2013 at 9:47 am
Jeff Moden (7/29/2013)
Eugene Elutin (7/29/2013)
Jeff Moden (7/29/2013)
Eugene Elutin (7/29/2013)
Solomon Rutzky (7/27/2013)
- 23847234872893475983479583749583749573945739 is all digits, is a valid number, but is NOT convertible to any SQL Server number types as it is larger than 38 digits
...
You can convert it to float (with lose of some precision ;-))
select cast('23847234872893475983479583749583749573945739' as float)
Considering that FLOAT only has 15 digits of precision, it'll be a pretty big loss. 🙂
I wouldn't call 1.32% a such "big loss" :hehe:
However you are right! It does depend! If this loss constitutes my interest in £ - I would probably die from heart-attack 😀
Heh... absolutely agreed but wasn't talking about the loss in "value" of the number. Was talking about the number of digits that would be lost when trying to determine if a long string could be checked for "IsAllDigits".
Hmm. I thought I had tested that one using CONVERT and that it errored, but I tried again and it worked. Thanks for mentioning that.
I agree that the loss of precision (i.e. rounding up) is non-ideal but seems to happen with the decimal types: MONEY, SMALLMONEY, DECIMAL / NUMERIC. So not a true conversion in the sense of being able to convert it back to the exact same string, but technically it does fit into the datatype. So it still fits into what I was saying regarding the need to determine if the value expressed in the string is really a number with respect to the end purpose of that number.
Take care,
Solomon..
SQL# — https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
Sql Quantum Lift — https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap — https://SqlQuantumLeap.com/ ( blog )
Info sites — Collations • Module Signing • SQLCLR
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply