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/