SQL ISNUMERIC function
ISNUMERIC(s) returns 1 when s converts to any numeric type, money and float included. That is rarely the question you meant to ask.
- Catégorie
- Conversion
- Returns
int (1 or 0)- In this category
- 37 of 39
Syntaxe
ISNUMERIC(expression)
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | Supported under this name. |
| PostgreSQL | Different name or syntax | pg_input_is_valid(s, 'numeric') from 16; a regex before that |
| MySQL | Different name or syntax | no function; s REGEXP '^-?[0-9]+([.][0-9]+)?$' |
| SQLite | Different name or syntax | no function; GLOB or typeof() after a cast |
| Oracle | Different name or syntax | VALIDATE_CONVERSION(s AS NUMBER) from 12.2 |
Exemples
SQL Server
SELECT ISNUMERIC('$') AS dollar, ISNUMERIC('12,3') AS comma, TRY_CAST('12,3' AS DECIMAL(10, 2)) AS as_decimal; -- 1, 1, NULL
PostgreSQL
SELECT pg_input_is_valid('12.5', 'numeric') AS a, pg_input_is_valid('12,3', 'numeric') AS b; -- t, f
MySQL
SELECT '12.5' REGEXP '^-?[0-9]+([.][0-9]+)?$' AS a, '12.5e3' REGEXP '^-?[0-9]+([.][0-9]+)?$' AS b; -- 1, 0
Oracle
SELECT VALIDATE_CONVERSION('12.5' AS NUMBER) AS a, VALIDATE_CONVERSION('12,5x' AS NUMBER) AS b FROM dual; -- 1, 0
Bon à savoir
- ISNUMERIC returns 1 for strings that do not convert to int or decimal: '$', '.', '-', '12,3' and CHAR(9) all give 1, because money or float accepts them. TRY_CAST(s AS DECIMAL(18, 4)) IS NOT NULL is the check most code needs.
- ISNUMERIC('') is 0, but TRY_CAST('' AS INT) is 0, not NULL: an empty string converts to integer zero on SQL Server. Cast to DECIMAL to get NULL.
- PostgreSQL's pg_input_is_valid ignores surrounding blanks: ' 7 ' is a valid integer.
- Oracle's VALIDATE_CONVERSION reads the decimal separator from NLS_NUMERIC_CHARACTERS, so '12.5' can fail in a session set to a comma decimal.
Questions about ISNUMERIC
Why does ISNUMERIC return 1 but CAST fails?
ISNUMERIC accepts anything some numeric type accepts. '$' is a valid money value and '1d2' a valid float, yet CAST('$' AS INT) fails. Test with TRY_CAST to the type you are converting to.
Is there TRY_CAST in PostgreSQL?
No. From PostgreSQL 16 use CASE WHEN pg_input_is_valid(s, 'numeric') THEN s::numeric END; on older versions match a regex first.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.