Apache Hive : CAST…FORMAT with SQL:2016 datetime formats

Usage

CAST(<timestamp/date> AS <varchar/char/string> [FORMAT <template>])  
CAST(<varchar/char/string> AS <timestamp/date> [FORMAT <template>])

Example

select cast(dt as string format 'DD-MM-YYYY')  
select cast('01-05-2017' as date format 'DD-MM-YYYY')

Template elements, a.k.a. Tokens, a.k.a Patterns a.k.a SQL:2016 Datetime Formats

Notes

For all tokens:

For all numeric tokens:

Accepted format tokens

Note: - “|” means “or”.

A.1. Numeric temporal tokens

YYYY
4-digit year

YYY
Last 3 digits of a year

YY
Last 2 digits of a year

Y
Last digit of a year

RRRR
4-digit rounded year

RR
2-digit rounded year
-String to datetime conversion:

MM
Month (1-12)

DD
Day of month (1-31)

DDD
Day of year (1-366)

HH
Hour of day (1-12)

HH12
Hour of day (1-12)
See HH.

HH24
Hour of day (0-23)

MI
Minute of hour (0-59)

SS
Second of minute (0-59)

SSSSS
Second of Day (0-86399)

FF[1..9]
Fraction of second

AM|A.M.|PM|P.M.
Meridiem indicator (or AM/PM)

D
Day of week (1-7)

Q
Quarter of year (1-4)

WW
Aligned week of year (1-53)

W
Aligned week of month (1-5)

IYYY
4-digit ISO 8601 week-numbering year

IYY
Last 3 digits of ISO 8601 week-numbering year

IY
Last 2 digits of ISO 8601 week-numbering year

I
Last digit of ISO 8601 week-numbering year

IW
ISO 8601 week of year (1-53)

ID
ISO 8601 day of week (1-7)

A.2. Character temporals

Temporal elements, but spelled out.

MONTH|Month|month
Name of month of year

MON|Mon|mon
Abbreviated name of month of year

DAY|Day|day
Name of day of week

DY|Dy|dy
Abbreviated name of day of week

B. Time zone tokens

TZH
Time zone offset hour (-15 to +15)

TZM
Time zone offset minute (0-59)

C. Separators

-|.|/|,|'|;|:|
Separator

D. ISO 8601 delimiters

T|Z
ISO 8601 delimiter

E. Nested strings (Text)

– Surround with double quotes (") in the pattern. Note, if the whole pattern string is delimited
by double quotes, then the double quotes must be escaped with a single backslash: (").

F. Format modifier tokens

FM
Fill mode modifier

FX
Format exact modifier