unix_timestamp() function ignores milliseconds. it returns seconds passed from unix epoch. Milliseconds are lost. For example:
select FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP('2020-01-21 15:15:35.125', 'yyyy-MM-dd HH:mm:ss.SSS') * 1000, 'CST6CDT');
returns:
2020-01-21 09:15:35
If the timestamp is in the right format 'yyyy-MM-dd HH:mm:ss.SSS' (and in your example, according to the template, it does not require conversion), this works fine:
with s as (select '2020-01-21 15:15:35.125' as added_date)
select FROM_UTC_TIMESTAMP(s.added_date, 'CST6CDT') from s;
Result:
2020-01-21 09:15:35.125
And if the timestamp is in the format which requires conversion, the workaround is to extract milliseconds from original timestamp and add to the milliseconds you got after multiplying unix_timestamp by 1000:
with s as (select '2020-01-21 15:15:35.125' as added_date)
select FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(s.added_date, 'yyyy-MM-dd HH:mm:ss.SSS') * 1000+cast(split(added_date,'\\.')[1] as int), 'CST6CDT') from s;
Result:
2020-01-21 09:15:35.125
One more workaround is to concatenate with milliseconds:
with s as (select '2020-01-21 15:15:35.125' as added_date)
select concat_ws('.',cast(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(s.added_date, 'yyyy-MM-dd HH:mm:ss.SSS') * 1000, 'CST6CDT') as string),split(added_date,'\\.')[1]) from s;
Result:
2020-01-21 09:15:35.125