In Hive SQL, to convert a string into a DATE
type, you use the TO_DATE
or CAST
/DATE
functions depending on the exact format of the string.
✅ Common Methods
-
If the string is already in
yyyy-MM-dd
formator
-
If the string has a custom format (e.g.
yyyyMMdd
,dd-MM-yyyy
, etc.)
First parse it into a timestamp withunix_timestamp
, then cast:
-
If you need TIMESTAMP instead of DATE
This will give a full
TIMESTAMP
rather than truncating to the date.
📌 Summary:
-
Use
TO_DATE(string)
if it’s alreadyyyyy-MM-dd
. -
Use
TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(str, format)))
if the string has another format. -
Use
CAST(... AS DATE)
when the string is ISO formatted.
In Hive SQL, the function FROM_UNIXTIME(unix_timestamp [, format])
is used to convert a UNIX timestamp (an integer number of seconds since 1970-01-01 00:00:00 UTC
) into a string (formatted date/time).
🔹 Behavior
-
Without format argument
→ Returns a string in
yyyy-MM-dd HH:mm:ss
format.
Example result: -
With format argument (custom output)
→ Returns a string formatted according to the given pattern.
Example result: -
Return type
-
Always returns a STRING (not DATE or TIMESTAMP).
-
If you want a
DATE
, wrap it withTO_DATE(...)
. -
If you want a
TIMESTAMP
, cast it explicitly:
-
In Hive SQL, both TO_DATE()
and CAST(... AS DATE)
can look similar but behave differently.
🔹 TO_DATE()
-
Input type: Takes a
STRING
orTIMESTAMP
. -
Behavior: Extracts only the date portion (
yyyy-MM-dd
) and discards the time part. -
Return type:
DATE
.
Examples:
🔹 CAST(... AS DATE)
-
Input type: Requires a
STRING
in standard ISO format (yyyy-MM-dd
) or aTIMESTAMP
. -
Behavior: Converts the value directly into DATE without extra parsing flexibility.
-
Return type:
DATE
.
Examples:
🔹 Key Differences
Function | Handles yyyy-MM-dd HH:mm:ss string | Flexible with formats (via unix_timestamp ) | Drops time part automatically | Requires strict ISO yyyy-MM-dd |
---|---|---|---|---|
TO_DATE() | ✅ Yes | ✅ Yes (with unix_timestamp ) |
✅ Always | ❌ Not strict |
CAST(... AS DATE) | ❌ No (needs pure yyyy-MM-dd ) |
❌ No | ✅ If input is timestamp | ✅ Strict ISO format only |
📌 Rule of thumb:
-
Use
TO_DATE
if your string may include time or is not strictly inyyyy-MM-dd
. -
Use
CAST(... AS DATE)
when you’re sure it’s already in ISO date format.