A varchar column in a table contains dates in '05/13/2019 20:48:13 PM' format. It can contain either a single date or many dates concatenated with pipe |. I use this query to get the very first one and try to convert it to timestamp.
Select (CASE WHEN charindex('AM', COMPL_DATE)>=1 OR charindex('PM', COMPL_DATE) >= 1
THEN TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh24:mi:ss AM')
ELSE TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh:mi:ss')
END
) from some_table.
I get this error Can't parse '03/23/2019 20:56:22 PM' as timestamp with format 'MM/DD/YYYY hh24:mi:ss AM'.
It is not that it fails for all the rows. There are some rows for which it fails. However when I use this data to run separately, like this -
with tab as ( select '03/23/2019 20:56:22 PM' COMPL_DATE from dual )
SELECT
(CASE WHEN charindex('AM',COMPL_DATE)>=1 OR charindex('PM',COMPL_DATE) >= 1
THEN TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh:mi:ss AM')
ELSE TO_TIMESTAMP ( SPLIT_PART( COMPL_DATE, '|' , 1), 'MM/DD/YYYY hh24:mi:ss')
END
) result
FROM tab ;
It works fine.
Any help is appreciated.
COMPL_DATE:: TIMESTAMP
– Krishna