postgresql字符串文本暴力转时间戳日期时间类型

postgresql字符串转时间戳,转换失败时返回null

create or replace function convert_datetime(date_str text) returns timestamp
    language plpgsql immutable as $D$
    declare result timestamp;
        arr text[];
    begin
        arr := regexp_match(date_str,'[^\d]{0,5}(\d{4})[^\d]{0,5}(\d{1,2})[^\d]{0,3}(\d{1,2})[^\d]{0,3}(\d{1,2})?[^\d]{0,3}(\d{1,2})?[^\d]{0,3}(\d{1,2})?','i');
        if $1 ilike '%pm%' then
            result = concat(concat_ws('-',coalesce(arr[1],'0'),coalesce(arr[2],'0'),coalesce(arr[3],'0')),' ',concat_ws(':',coalesce((arr[4]::int+12)::text,'0'),coalesce(arr[5],'0'),coalesce(arr[6],'0')));
        else
            result = concat(concat_ws('-',coalesce(arr[1],'0'),coalesce(arr[2],'0'),coalesce(arr[3],'0')),' ',concat_ws(':',coalesce(arr[4],'0'),coalesce(a

你可能感兴趣的:(pg,数据库,postgresql)