PostgreSQL:如何更改输出中的年龄格式

PostgreSQL: How to change the age Format in the output

我在 患者 table 中有一个 patient_age 列 以及该列中的年龄格式会是这样,

10D8M45Y 

(表示10 Days 8 M45Y耳)

我需要一个可以给出这样输出的 Postgresql 查询,

45 Years 8 Months 10 Days

谢谢。

该列中的年龄格式如下 'YY "Years" mm "Months" DD "Days".

SELECT  TO_CHAR(age(current_date, '2017-04-17 00:00:00.0'), 'YY "Years" mm "Months" DD "Days"') as length_of_service

如上所述,您希望像这样计算查询时的年龄(如果可能)...

SELECT extract(year from AGE(end_date, NOW()))||' Years '||
    extract(month from AGE(end_date, NOW()))||' Months '||
    extract(day from AGE(end_date, NOW()))||' Days '||
    extract(hour from AGE(end_date, NOW()))||' Hours '||
    extract(minute from AGE(end_date, NOW()))||' Minutes' as formatted_age
FROM my_table

您可能正在处理一些硬编码到字段中的内容。

SELECT
    extract(DAY FROM INTERVAL '10D8M45Y') || ' days ' ||
    extract(MONTH FROM CAST(REPLACE('10D8M45Y', 'M', 'Mon') AS INTERVAL)) || ' months ' ||
    extract(YEAR FROM INTERVAL '10D8M45Y') || ' years ';