PostgreSQL通过日期类型转换完成日期格式统一

一、原数据样式

PostgreSQL通过日期类型转换完成日期格式统一_第1张图片

 二、查询

原理:筛选符合日期格式的写法,通过类型转换转为日期,如果筛选的结果仅有年月,则补充为该月的1号再进行转换

SELECT 
"Primary Completion Date",
CASE
WHEN "Primary Completion Date" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Primary Completion Date" ::date
WHEN "Primary Completion Date" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Primary Completion Date"||',1') ::date
END AS "处理后"
FROM
"be_clinical status";

 三、更新

UPDATE
"be_clinical status"
SET
"Primary Completion Date"=(
CASE
WHEN "Primary Completion Date" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Primary Completion Date" ::date
WHEN "Primary Completion Date" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Primary Completion Date"||',1') ::date
END)
;

四、批量更新

UPDATE
"be_clinical status"
SET
"Primary Completion Date"=(
CASE
WHEN "Primary Completion Date" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Primary Completion Date" ::date
WHEN "Primary Completion Date" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Primary Completion Date"||',1') ::date
END),
"Start Date"=(
CASE
WHEN "Start Date" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Start Date" ::date
WHEN "Start Date" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Start Date"||',1') ::date
END),
"Completion Date"=(
CASE
WHEN "Completion Date" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Completion Date" ::date
WHEN "Completion Date" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Completion Date"||',1') ::date
END),
"First Posted"=(
CASE
WHEN "First Posted" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "First Posted" ::date
WHEN "First Posted" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("First Posted"||',1') ::date
END),
"Last Update Posted"=(
CASE
WHEN "Last Update Posted" ~'[a-z0-9]+[ ,-]+[a-z0-9]+[ ,-]+[a-z0-9]+$' THEN "Last Update Posted" ::date
WHEN "Last Update Posted" ~'[a-z0-9]+ [a-z0-9]+$' THEN ("Last Update Posted"||',1') ::date
END)
;

五、处理结果

PostgreSQL通过日期类型转换完成日期格式统一_第2张图片

你可能感兴趣的:(药智数据处理)