SQL Server ISNUMERIC() Function
The SQL Server (Transact-SQL) ISNUMERIC() function is used to test whether an expression is a valid numeric type. The function returns 1 if the expression is a valid numeric type, else returns 0.
Syntax
ISNUMERIC(expression)
Parameters
expression |
Required. Specify the value to test for valid numeric type. |
Return Value
Returns 1 if the expression is a valid numeric type. Returns 0 otherwise.
Example 1:
The example below shows the usage of ISNUMERIC() function.
SELECT ISNUMERIC('Paris'); Result: 0 SELECT ISNUMERIC(NULL); Result: 0 SELECT ISNUMERIC(0); Result: 1 SELECT ISNUMERIC(10); Result: 1 SELECT ISNUMERIC(10.25); Result: 1 SELECT ISNUMERIC('10.25'); Result: 1
Example 2:
Consider a database table called Product with the following records:
ProductName | Price | StockQuantity | OrderQuantity |
---|---|---|---|
Apple | 1.00 | 100 | 20 |
Banana | 1.25 | 120 | 30 |
Orange | 2.15 | 105 | NULL |
Watermelon | 3.50 | 75 | 15 |
If the OrderQuantity is optional and can contain NULL values. The statement mentioned below will give NULL value.
SELECT *, Price * (StockQuantity + OrderQuantity) AS Inventory FROM Product;
This will produce the result as shown below:
ProductName | Price | StockQuantity | OrderQuantity | Inventory |
---|---|---|---|---|
Apple | 1.00 | 100 | 20 | 120.0 |
Banana | 1.25 | 120 | 30 | 187.5 |
Orange | 2.15 | 105 | NULL | NULL |
Watermelon | 3.50 | 75 | 15 | 315.0 |
To avoid such situations, the ISNUMERIC() function can be used in conjunction with IIF() function to provide an alternative value to the column if it contains NULL value.
SELECT *, Price * (StockQuantity + IIF(ISNUMERIC(OrderQuantity)=0, 0, OrderQuantity)) AS Inventory FROM Product;
This will produce the result as shown below:
ProductName | Price | StockQuantity | OrderQuantity | Inventory |
---|---|---|---|---|
Apple | 1.00 | 100 | 20 | 120.0 |
Banana | 1.25 | 120 | 30 | 187.5 |
Orange | 2.15 | 105 | NULL | 225.75 |
Watermelon | 3.50 | 75 | 15 | 315.0 |
❮ SQL Server Functions