SELECT concat(
substr(date_str, 7, 4), '-',
substr(date_str, 4, 2), '-',
substr(date_str, 1, 2)
) AS formatted_date
FROM your_table;
This converts dd/MM/yyyy strings into yyyy-MM-dd strings compatible with Impala date/timestamp functions.
'yyyy-MM-dd HH:mm:ss'
If you want it explicitly as a TIMESTAMP type, this will work as well because the returned string can be automatically cast to TIMESTAMP in many cases.
If you want just the date part without time, you can wrap it with date() function:
sql
SELECT date(from_unixtime(unix_timestamp('21/06/2024, 08:58 AM', 'dd/MM/yyyy, hh:mm a'))) AS parsed_date;
Summary:
sql
-- For full datetime (timestamp)
SELECT from_unixtime(unix_timestamp('21/06/2024, 08:58 AM', 'dd/MM/yyyy, hh:mm a')) AS datetime_val;
-- For date only
SELECT date(from_unixtime(unix_timestamp('21/06/2024, 08:58 AM', 'dd/MM/yyyy, hh:mm a'))) AS date_val;
This will parse your string correctly in Impala.
Impala's unix_timestamp and date/time functions support a limited set of format patterns compared to, for example, Java's SimpleDateFormat. Unfortunately, Impala does not support the a (AM/PM) marker directly in unix_timestamp format strings, so the format 'dd/MM/yyyy, hh:mm a' will cause the error you've seen.
Workarounds
Since the input string is:
code
21/06/2024, 08:58 AM
And Impala can't directly parse the AM/PM, you have two options:
Option 1: Convert AM/PM to 24-hour format outside SQL or preprocess the data
E.g., preprocess the string in your ETL process, or in your SQL query by writing a UDF or complicated logic. This is often cumbersome.
Option 2: Use string functions in Impala to normalize the input string and parse
Since Impala cannot parse AM/PM in unix_timestamp, the workaround is:
Extract the AM/PM portion.
Extract the time components.
Convert hh:mm AM/PM to 24-hour HH:mm format manually.
Rebuild a string with yyyy-MM-dd HH:mm:ss format to parse.
Let's do this step by step in SQL:
Sample input:
sql
SELECT '21/06/2024, 08:58 AM' AS input_str;
Step 1: Extract date and time parts
Split the string:
sql
SELECT
substr(input_str, 1, 10) AS date_part, -- '21/06/2024'
substr(input_str, 13, 5) AS time_part, -- '08:58'
substr(input_str, 19, 2) AS ampm_part -- 'AM'
FROM (SELECT '21/06/2024, 08:58 AM' AS input_str) t;
Step 2: Convert date_part to yyyy-MM-dd (Impala likes yyyy-MM-dd)
You can use regexp_replace() or regexp_extract() to rearrange the dd/MM/yyyy to yyyy-MM-dd.
Impala approach:
sql
SELECT
concat(
substr(date_part, 7, 4), '-', -- yyyy
substr(date_part, 4, 2), '-', -- MM
substr(date_part, 1, 2) -- dd
) AS formatted_date
FROM (
SELECT substr('21/06/2024, 08:58 AM', 1, 10) AS date_part
) t;
This returns: 2024-06-21
Step 3: Convert 12-hour time + AM/PM to 24-hour time
We need to convert 08:58 AM to 08:58:00 (which is already ok for AM before 12);
But if it was e.g. 08:58 PM, we have to add 12 hours to 08 (=> 20). For 12 AM it becomes 00.
Let's write a CASE expression:
sql
SELECT
time_part,
ampm_part,
CASE
WHEN ampm_part = 'AM' AND substr(time_part, 1, 2) = '12' THEN concat('00', substr(time_part, 3, 3)) -- 12 AM => 00:mm
WHEN ampm_part = 'AM' THEN concat(time_part, ':00') -- other AM times same
WHEN ampm_part = 'PM' AND substr(time_part, 1, 2) = '12' THEN concat(time_part, ':00') -- 12 PM same 12:mm:ss
WHEN ampm_part = 'PM' THEN concat(cast(cast(substr(time_part, 1, 2) as int) + 12 as string), substr(time_part, 3, 3), ':00')
ELSE NULL
END AS time_24h
FROM (
SELECT
substr('21/06/2024, 08:58 AM', 13, 5) AS time_part,
substr('21/06/2024, 08:58 AM', 19, 2) AS ampm_part
) t;
Explanation:
substr(time_part, 1, 2) extracts hour
substr(time_part, 3, 3) extracts :mm
We append :00 seconds to match HH:mm:ss
Example:
For '08:58 AM' returns '08:58:00'
For '08:58 PM' returns '20:58:00'
For '12:30 AM' returns '00:30:00'
For '12:30 PM' returns '12:30:00'
Step 4: Concatenate the formatted date and converted time
sql
SELECT
concat(
cast(substr(input_str, 7, 4) AS string), '-', -- yyyy
substr(input_str, 4, 2), '-', -- MM
substr(input_str, 1, 2), ' ', -- dd + space
time_24h
) AS datetime_24h
FROM (
SELECT
'21/06/2024, 08:58 AM' AS input_str,
substr('21/06/2024, 08:58 AM', 13, 5) AS time_part,
substr('21/06/2024, 08:58 AM', 19, 2) AS ampm_part,
CASE
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'AM' AND substr('21/06/2024, 08:58 AM', 13, 2) = '12' THEN concat('00', substr('21/06/2024, 08:58 AM', 15, 3), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'AM' THEN concat(substr('21/06/2024, 08:58 AM', 13, 5), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'PM' AND substr('21/06/2024, 08:58 AM', 13, 2) = '12' THEN concat(substr('21/06/2024, 08:58 AM', 13, 5), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'PM' THEN concat(cast(cast(substr('21/06/2024, 08:58 AM', 13, 2) AS int) + 12 AS string), substr('21/06/2024, 08:58 AM', 15, 3), ':00')
ELSE NULL
END AS time_24h
) t;
Result should look like:
code
2024-06-21 08:58:00
Step 5: Finally cast to TIMESTAMP
sql
SELECT cast(datetime_24h AS timestamp) AS timestamp_col
FROM (
SELECT
concat(
cast(substr(input_str, 7, 4) AS string), '-', -- yyyy
substr(input_str, 4, 2), '-', -- MM
substr(input_str, 1, 2), ' ', -- dd + space
time_24h
) AS datetime_24h
FROM (
SELECT
'21/06/2024, 08:58 AM' AS input_str,
substr('21/06/2024, 08:58 AM', 13, 5) AS time_part,
substr('21/06/2024, 08:58 AM', 19, 2) AS ampm_part,
CASE
WHEN substr('21/06/2024', 7, 2) IS NULL THEN NULL -- just a fallback
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'AM' AND substr('21/06/2024, 08:58 AM', 13, 2) = '12' THEN concat('00', substr('21/06/2024, 08:58 AM', 15, 3), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'AM' THEN concat(substr('21/06/2024, 08:58 AM', 13, 5), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'PM' AND substr('21/06/2024, 08:58 AM', 13, 2) = '12' THEN concat(substr('21/06/2024, 08:58 AM', 13, 5), ':00')
WHEN substr('21/06/2024, 08:58 AM', 19, 2) = 'PM' THEN concat(cast(cast(substr('21/06/2024, 08:58 AM', 13, 2) AS int) + 12 AS string), substr('21/06/2024, 08:58 AM', 15, 3), ':00')
ELSE NULL
END AS time_24h
) t
) final;
Summary
Impala does not support parsing AM/PM directly, so you must manually convert 12-hour to 24-hour time via string functions, then concatenate a properly formatted datetime string and cast it to TIMESTAMP.
If you do this transformation once for your input string, this approach will work.
If you want, I can write a full generic reusable SQL snippet/function for you based on this logic. Just ask!