0

I am new to Spark. Any help will be appreciated. Is there any alternative for sql's FORMAT() in Spark SQL. My core logic is written as SQL and running with spark.sql("query"). I have a requirement to convert the id to 4 digit.

For example, if if is 1, it should be converted to 0001, if it is 12, then 0012. I know in SQL, we can do it as FORMAT("%04d", id) as id. But this is giving me error in Spark SQL saying FORMAT is not a function registered. Found format_number and format_string in Spark's documentation, but not helping in my case.

Note: I don't want to do this in my java code, but would like to do in SQL query itself.

1

1 Answer 1

1

Use lpad function.

spark.sql("SELECT lpad('1', 4, '0')").show

+-------------+
|lpad(1, 4, 0)|
+-------------+
|         0001|
+-------------+

You can change '1' to id.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.