Google Sheets 中的 DATEVALUE 在 Postgres 数据库中的等价物是什么?

huangapple go评论56阅读模式
英文:

What is the equivalent of DATEVALUE in Google sheets to Postgres database?

问题

在Google表格中,有一列使用公式DATEVALUE来将日期转换为数字值。

在Google表格中,DATEVALUE('2023-18-05')等于45064

需要在Postgres中实现类似的输出。

请问在Postgres中有没有相同的函数?如果没有等效的函数,如何在Postgres中解决这个问题?

英文:

I am trying to replicate a report from Google sheets to Postgres. In Google sheets one of the column is calculated using the formulae DATEVALUE which converts date to a numeric value.

In Google sheets DATEVALUE('2023-18-05') is equal to 45064

Need to achieve similar output in Postgres.

What is the equivalent function for the same in Postgres?
If there is no equivalent function how to approach to solve the same in Postgres?

答案1

得分: 2

  1. 这个'2023-18-05'看起来不像是有效的格式,当我在Google Sheets中尝试时失败了。它不应该是'2023-05-18'吗?

  2. 在这种情况下,输出45064是特定于Google Sheets的,详细信息请参见Formats,它表示自1899-12-30以来的天数。

鉴于第2点,您可以封装为一个函数:

CREATE OR REPLACE FUNCTION public.google_days(in_dt date)
 RETURNS numeric
 LANGUAGE sql
AS $function$
select in_dt - '1899-12-30'::date;
$function$

select google_days('2023-5-18');
 google_days 
-------------
       45064
英文:
  1. This '2023-18-05' does not look like a valid format and when I try it in Google Sheets it fails. Should it not be '2023-05-18'?.
  2. The output in this case 45064 is Google Sheets specific as detailed here Formats and is the days from 1899-12-30

Given 2) then:

select '2023-05-18'::date - '1899-12-30'::date;
45064

Which you could encapsulate in a function:

CREATE OR REPLACE FUNCTION public.google_days(in_dt date)
 RETURNS numeric
 LANGUAGE sql
AS $function$
select in_dt - '1899-12-30'::date;
$function$

select google_days('2023-5-18');
 google_days 
-------------
       45064

huangapple
  • 本文由 发表于 2023年5月25日 03:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定