Analyzing string is not a number sql

There sql2005 the ISNUMERIC function (expression) function: When the expression is a number, returns a 0 otherwise. It's just a rookie-level solution, in most cases relatively effective.

eg:

SELECT  the ISNUMERIC ( ' 123 ' ) - the result is a

However, the function has a drawback!

eg:

Copy the code
 SELECT 
 ,ISNUMERIC('-') as '-'    --1
 ,ISNUMERIC('+') as '+'    --1
 ,ISNUMERIC('$') as '$'    --1
 ,ISNUMERIC('.') as '.'    --1
 ,ISNUMERIC(',') as ','    --1
 ,ISNUMERIC('\') as '\'    --1
 ,ISNUMERIC('2D3') AS '2D3'--1
,ISNUMERIC('1d1') AS '1d1'--1
,ISNUMERIC('1e1') AS '1e1'--1
,ISNUMERIC('d') AS 'd'   --0
Copy the code

When present dollar sign, a minus sign, symbols such as commas, or when D, both before and after the occurrence numbers E, also returns 1 , which is a headache. Punctuation is actually easy to understand why d, e this case, Microsoft really do not understand the intent of the design.

What is a good solution? Of course, consider the following

Methods: wildcards. High force grid use.

Applicable scene: 2005 and above (2005 version have not tried before, should also support)

 - Back was pure digital 0- (support positive and negative numbers, decimal) 
 the SELECT  the PATINDEX ( ' % [^ 0-9 | | - |. +]% ' , ' 2.2 ' ) - returns 0

 - Returns an integer of 0- was pure 
SELECT  the PATINDEX ( ' % [^ 0-9]% ' , ' 2.2 ' ) - Non-zero

Guess you like

Origin blog.csdn.net/qq_42335056/article/details/80819953