创建一个postgresql函数,该功能返回字符串' fall',' spring'取决于年份

发布于 2025-01-25 15:53:31 字数 358 浏览 2 评论 0原文

我需要创建一个函数,该功能根据一年的月份返回字符串“秋季”或“春季”。如果该函数被命名为getTerm,并且没有参数,则我想在这样的选择语句中使用它:

选择名称,className,从classtable

classtable保留我们提供的类的名称。结果集将包含如下的列:

-Jack,Data Systems,SP2022

-Jill,Web Quits,F2023

我使用了NOW()函数。我也可以使用提取物(从现在开始()()来获取我的当前季度。然后,使用“ if”或“ case”子句返回“春季”或“秋天”似乎很简单。我只是没有找到这样的函数的示例。

谁能建议一些示例代码?

I need to create a function that returns the string 'fall' or 'spring' depending on the month of the year. If the function was named getterm and took no parameters I would like to use it in a select statement like this:

select name, classname, getterm from classtable

where classtable holds the names of the classes we offer. The result set would include the columns as follows:

-Jack, Data Systems, Sp2022

-Jill, Web Stuff, F2023

I have used the now() function. I can also use extract(quarter from now()) to get my current quarter. It would seem simple then to use an 'if' or 'case' clause to return 'spring' or 'fall' based upon the quarters. I just haven't found any examples of a function like this.

Can anyone suggest some sample code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2025-02-01 15:53:31

每个文档从这里

有一种“简单”的案例表达式,是上述一般形式的变体:

CASE expression
    WHEN value THEN result
    [WHEN ...]
    [ELSE result]
END

计算第一个表达式,然后将其与wher whes ense等于它等于它的每个值表达式进行比较。如果找不到匹配,则返回其他子句(或null值)的结果。这类似于c。

中的开关语句

这在您的情况下转化为:

SELECT
    CASE extract('quarter' FROM now())
    WHEN 1 THEN
        'winter'
    WHEN 2 THEN
        'spring'
    WHEN 3 THEN
        'summer'
    WHEN 4 THEN
        'fall'
    END;

 case  
--------
 spring

Per documentation from here CASE:

There is a “simple” form of CASE expression that is a variant of the general form above:

CASE expression
    WHEN value THEN result
    [WHEN ...]
    [ELSE result]
END

The first expression is computed, then compared to each of the value expressions in the WHEN clauses until one is found that is equal to it. If no match is found, the result of the ELSE clause (or a null value) is returned. This is similar to the switch statement in C.

This translates in your case to:

SELECT
    CASE extract('quarter' FROM now())
    WHEN 1 THEN
        'winter'
    WHEN 2 THEN
        'spring'
    WHEN 3 THEN
        'summer'
    WHEN 4 THEN
        'fall'
    END;

 case  
--------
 spring
邮友 2025-02-01 15:53:31

谢谢Adrian ..我现在有工作功能:

create function getTermString() returns text as $
select case extract(quarter from now()) 
when 1 then 'sp'
when 2 then 'sp'
when 3 then 'f'
when 4 then 'f'
end ;
$ language SQL;

Thank you to Adrian.. I now have the working function:

create function getTermString() returns text as $
select case extract(quarter from now()) 
when 1 then 'sp'
when 2 then 'sp'
when 3 then 'f'
when 4 then 'f'
end ;
$ language SQL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文