How To Format Date In Spark Sql?
I need to transform this given date format: 2019-10-22 00:00:00 to this one: 2019-10-22T00:00:00.000Z I know this could be done in some DB via: In AWS Redshift, you can achieve thi
Solution 1:
This is the natural way I think.
spark.sql("""SELECT date_format(to_timestamp("2019-10-2200:00:00", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") as date""").show(false)
The result is:
+------------------------+
|date |
+------------------------+
|2019-10-22T00:00:00.000Z|
+------------------------+Solution 2:
Maybe something like this? It's a bit different approach.
scala> val df = spark.range(1).select(current_date.as("date"))
scala> df.show()
+----------+| date|+----------+|2019-11-09|+----------+
scala>
df.withColumn("formatted",
concat(
regexp_replace(date_format('date,"yyyy-MM-dd\tHH:mm:ss.SSS"),"\t","T"),
lit("Z")
)
).show(false)
+----------+------------------------+|date |formatted |+----------+------------------------+|2019-11-09|2019-11-09T00:00:00.000Z|+----------+------------------------+
Post a Comment for "How To Format Date In Spark Sql?"