Convert String to Date using Spark SQL

visibility 14,552 access_time 2 years ago languageEnglish

In Spark, function to_date can be used to convert string to date. This function is available since Spark 1.5.0.

Code snippet

SELECT to_date('2020-10-23', 'yyyy-MM-dd');
SELECT to_date('23Oct2020', 'ddMMMyyyy');

Datetime patterns

Refer to the official documentation about all the datetime patterns.

GeratextAD; Anno Domini
yyearyear2020; 20
M/Lmonth-of-yearmonth7; 07; Jul; July
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Eday-of-weektextTue; Tuesday
Faligned day of week in monthnumber(1)3
hclock-hour-of-am-pm (1-12)number(2)12
Khour-of-am-pm (0-11)number(2)0
kclock-hour-of-day (1-24)number(2)0
Hhour-of-day (0-23)number(2)0
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset ‘Z’ for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;
escape for textdelimiter 
’‘single quoteliteral
[optional section start  
]optional section end  

copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

timeline Stats
Page index 25.26
More from Kontext
Spark - Read and Write Data with MongoDB
visibility 1,115
thumb_up 0
access_time 8 months ago
Spark - Read and Write Data with MongoDB
Teradata SQL - First Day of a Month
visibility 904
thumb_up 0
access_time 2 years ago