2

Like we have SQL ISNUMERIC Function which validates whether the expression is numeric or not , I need if there is any equivalent function in Spark SQL, I have tried to find it but couldn't get it. Please if someone can help or suggest for the same ?

4
  • So, do you need to filter the Rows after made the SQL execution?? Commented Jun 23, 2019 at 18:15
  • Possible duplicate of Possible to filter Spark dataframe by ISNUMERIC function? Commented Jun 23, 2019 at 18:18
  • 1
    Yes but, that solution is in scale. I guess he is wondering in Java. Well, he should specific which language. Commented Jun 23, 2019 at 19:28
  • I need to use isnumeric function in Azure databricks where I am using spark sql Commented Jul 7, 2019 at 19:16

2 Answers 2

4

For anyone coming here by way of Google :) , there is an alternative answer by regex for isnumeric in spark sql

select OldColumn, CASE WHEN OldColumn not rlike '[^0-9]' THEN 1 ELSE 0 END AS OldColumnIsNumeric from table 

The regex simply checks if the column is numeric or not.

You can modify to fit for substrings of the column you are checking too.

Sign up to request clarification or add additional context in comments.

Comments

3

Try using spark udf, this approach will help you clone any function -

scala> spark.udf.register("IsNumeric", (inpColumn: Int) => BigInt(inpColumn).isInstanceOf[BigInt]) res46: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,BooleanType,Some(List(IntegerType))) scala> spark.sql(s""" select "ABC", IsNumeric(123) as IsNumeric_1 """).show(false) +---+-----------+ |ABC|IsNumeric_1| +---+-----------+ |ABC|true | +---+-----------+ scala> spark.sql(s""" select "ABC", IsNumeric("ABC") as IsNumeric_1 """).show(false) +---+-----------+ |ABC|IsNumeric_1| +---+-----------+ |ABC|null | +---+-----------+ 

Here, above function will return null if column value is not integer.

Hope this will be helpful.

2 Comments

In where clause as well?
Yes you can use spark udf in where clause.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.