I am working in SQL Server 2008. I am trying to test whether a string (varchar) has only digit characters (0-9). I know that the IS_NUMERIC function can give spurious results. (My data can possibly have $ signs, which should not pass the test.) So, I'm avoiding that function.
I already have a test to see if a string has any non-digit characters, i.e.,
some_column LIKE '%[^0123456789]%'
I would think that the only-digits test would be something similar, but I'm drawing a blank. Any ideas?
8条答案
按热度按时间hgncfbus1#
The selected answer does not work.
I don't have a solution but know of this potential pitfall. The same goes if you substitute the letter 'D' for 'E' which is scientific notation.
pxq42qpu2#
ITS WORKS!!!
flmtquvp3#
Use
Not Like
Demo
1wnzp6jl4#
There is a system function called ISNUMERIC for SQL 2008 and up. An example:
I did a couple of quick tests and also looked further into the docs:
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0.
Which means it is fairly predictable for example
-9879210433
would pass but987921-0433
does not.$9879210433
would pass but9879210$433
does not.So using this information you can weed out based on the list of valid currency symbols and
+
&-
characters.5vf7fwbs5#
Solution:
where some_column NOT LIKE '%[^0-9]%'
Is correct.Just one important note: Add validation for when the string column =
''
(empty string). This scenario will return that''
is a valid number as well.qhhrdooz6#
Method that will work. The way it is used above will not work.
slmsl1lt7#
8zzbczxx8#
I was attempting to find strings with numbers ONLY, no punctuation or anything else. I finally found an answer that would work here .
Using PATINDEX('%[^0-9]%', some_column) = 0 allowed me to filter out everything but actual number strings.