簡體   English   中英

PostgreSQL - 如何將數字格式化為自定義時間(小時,分鍾)

[英]PostgreSQL - how to format number into custom time(hours, minutes)

我很難弄清楚如何選擇列作為自定義時間格式。 我有一列可以說“時間”,行值為“70”,我想將其格式化為

1h 10min

當值將在示例“50”中時,我需要這樣的輸出:

50min

我已經閱讀了有關 HH:MI:SS 等格式的信息,但我想將“h”包含在幾小時內,將“min”包含在幾分鍾內。

最簡單的方法是轉換為區間或使用除法。 像這樣的東西(兩個選項+ 1個自定義格式):

    with t as (select 70 as minutes
               union
               select 210
               union
               select 50
               union
               select 0
               union
               select 10000)
    select
        t.minutes,
        (t.minutes || 'minutes')::interval as full_time,
        make_interval(mins => t.minutes) as full_time2,
        t.minutes/60 || 'h ' || mod(t.minutes,60) || 'min' as custom,
case when t.minutes < 60 then '' else t.minutes/60 || 'h ' end || mod(t.minutes,60) || 'min' as custom2
    from t
    order by t.minutes;

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM