What is wrong with spark sql substring function?

Viewed 17595

This should require no explanation. But could someone describe the logic behind the pos parameter of substring because I cannot make sense of this (Using Spark 2.1):

scala> val df = Seq("abcdef").toDS()
df: org.apache.spark.sql.Dataset[String] = [value: string]

scala> df.show
+------+
| value|
+------+
|abcdef|
+------+

scala> df.selectExpr("substring(value, 0, 2)", "substring(value, 1, 2)", "substring(value, 2,2)", "substring(value, 3,2)").show
+----------------------+----------------------+----------------------+----------------------+
|substring(value, 0, 2)|substring(value, 1, 2)|substring(value, 2, 2)|substring(value, 3, 2)|
+----------------------+----------------------+----------------------+----------------------+
|                    ab|                    ab|                    bc|                    cd|
+----------------------+----------------------+----------------------+----------------------+
1 Answers

first value is from what index it should start (starts from 1 not from 0) second value is how many characters it should take from the index

Related