The tricky SQL Server function ISNUMERIC to detect numeric types

A few days ago, I encountered a very difficult problem. A colleague proposed to calculate the start and end numbers in the column of character type , which is generally like this

New starting number = previous ending number + 1

New end number = new start number + quantity

And the data in this column is like this

in stock

CN003?005007

000890670000

98111100 stock

370111900000

001,800,900,00

~~~~~~~~~~~~

Wooden method, ask Du Niang, I found that there are still many experts , and proposed to use ISNUMERIC (column name) = 1, which is a numeric type, but please take a look at the documentation of ISNUMERIC

ISNUMERIC

 

Syntax

ISNUMERIC ( expression )

Parameters
expression
The expression to evaluate.

Return Type
int

Remarks
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise, it returns 0. Valid numeric data types include the following types: int,numeric,bigint,money,smallint,smallmoney,

tinyint,float,decimal,real

 

Note:
ISNUMERIC will return 1 for characters that are not numbers (such as plus (+), minus (-)) and valid currency symbols (such as dollar ($)) characters. A return value of 1 ensures that expression can be converted to one of the above numeric types. But there is a bug in this function. When the judged expression is a character type expression, it will be messed up! ' 001,800,900 'will also be considered as a numeric type

An alternative writing is

PATINDEX ('%[^0-9]%', index)

If the return value is equal to 0, it is a pure number (no characters other than 0~9).

case when PATINDEX('%[^0-9]%', 列名)=0 then cast (列名 as decimal(18,0))+cast(1 as decimal(18,0))
else ~~~ end as 起号

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327104751&siteId=291194637