How to identify strings that can be treated as numbers in PostgreSQL?

In PostgreSQL, you can apply the isnumeric function to identify strings that PostgreSQL can treat as numbers. The isnumeric function returns TRUE if all characters in the string are numeric, and FALSE otherwise.

Here is an example of how to use the isnumeric function in PostgreSQL:

SELECT isnumeric(‘123’); — Returns TRUE
SELECT isnumeric(‘1.23’); — Returns FALSE
SELECT isnumeric(‘1,000’); — Returns FALSE

In the first example, the isnumeric function returns TRUE because the string ‘123’ contains only numeric characters.
Conversely, in the second example, it returns FALSEsince string ‘1.23’ includes a non-numeric character (.).
Likewise, the third example also returns FALSE because the string ‘1,000’ includes character (`,`), which is not numeric.

Real-life data examples of using the isnumeric function in PostgreSQL can be found in various applications that handle data analysis or data validation. For example, a financial application might use the isnumeric function to validate input data before processing it, to ensure that only valid numeric data is used in financial calculations. Similarly, a data analytics application might use the isnumeric function to filter out non-numeric data before performing statistical analysis, to ensure that only valid data is used in the analysis.

Real-World Applications of isnumeric in PostgreSQL

The isnumeric function proves useful in many real-life scenarios where data validation is critical. For instance:

  • A financial application can validate user input before performing calculations, ensuring only proper numeric values are processed.

  • A data analytics pipeline can filter out non-numeric strings before running statistical operations, which helps prevent calculation errors or misinterpretations.

Here’s an example SQL query that demonstrates the use of the isnumeric function to filter out non-numeric data:

SELECT *
FROM my_table
WHERE isnumeric(my_column) = TRUE;

In this query:

  • my_table refers to the table you want to query.

  • my_column is the column containing mixed data – the data to be filtered.

The WHERE clause ensures that only rows with purely numeric values in my_column are returned.

By using the isnumeric function in this way, you can ensure that only valid numeric data is used in your data analysis or calculations.

FAQ

Q1: What is the isnumeric ( ) function in PostgreSQL?
A1: The isnumeric ( ) function checks if a string contains only numeric characters, returning TRUE if it does and FALSE otherwise.

Q2: Can isnumeric ( ) detect decimal numbers like ‘1.23’?
A2: No, isnumeric ( ) returns FALSE for strings with decimal points or commas, as it checks for digits only.

Q3: How can I filter rows with numeric strings in a PostgreSQL table?
A3: Use a WHERE clause with isnumeric ( ) to filter rows. For example: SELECT * FROM my_table WHERE isnumeric ( my_column) = TRUE; 

 

About Shiv Iyer 501 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.