0

I am trying to extract year part from date using extract in spark sql

     spark.sql("select extract(year from datecol) as dt from temp").show(false)

I am getting below exception for this.

    org.apache.spark.sql.catalyst.parser.ParseException:missing ')' at 'from'(line 1, pos 20)
    
2
  • just use select year(datecol) as dt Commented May 21, 2021 at 16:09
  • which Spark version do you use? Commented May 21, 2021 at 19:40

2 Answers 2

1

extract(field FROM source) - Extracts a part of the date/timestamp or interval source

This function available spark version 3.0.0 & lower version of spark it is not available hence you will get below exception if use extract function.

scala> spark.sql("select extract(year from datecol) as dt from tmp").show(false)
org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input 'from' expecting {')', ','}(line 1, pos 20)

== SQL ==
select extract(year from datecol) as dt from temp
--------------------^^^

  at org.apache.spark.sql.catalyst.parser.ParseException.withCommand(ParseDriver.scala:217)
  at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parse(ParseDriver.scala:114)
  at org.apache.spark.sql.execution.SparkSqlParser.parse(SparkSqlParser.scala:48)
  at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parsePlan(ParseDriver.scala:68)
  at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:623)
  ... 50 elided

Use year function.

spark.sql("select year(datecol) as dt from tmp").show(false)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use year function to extract year from date. For instance, if your date is in format 21-04-17 :

df1.withColumn("year", year(to_timestamp($"datecol", "yy-MM-dd")))

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.