Can someone help me resolve an sql syntax error on pgadmin while creating a function?

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

Can someone help me resolve an sql syntax error on pgadmin while creating a function?

问题

第一次尝试 -

```sql
CREATE FUNCTION rental_quarter (rental_date TIMESTAMP)
RETURNS VARCHAR(7)
BEGIN 
 IF MONTH(rental_date) BETWEEN 1 AND 3 THEN RETURN YEAR(rental_date)+ "Q1";
 ELSE IF MONTH(rental_date) BETWEEN 4 AND 6 THEN RETURN YEAR(rental_date)+ "Q2" ; 
 ELSE IF MONTH(rental_date) BETWEEN 7 AND 9 THEN RETURN YEAR(rental_date)+ "Q3";
 ELSE IF MONTH(rental_date) BETWEEN 10 AND 12 THEN RETURN YEAR(rental_date)+ "Q4";
 ELSE RETURN NULL ;
END IF;
END;

最新尝试 代码和错误

编辑 建议修复错误 #1

我对SQL很陌生,已经在这上面卡了几个小时了。我正在尝试创建一个rental_quarter(rental_date)函数,将时间戳日期格式(没有时区)转换为年份+季度。我完全不知道我做错了什么。我尝试过使用AS、DECLARE、SET AS。我已经尝试了各种分号的变化。它必须是一个用户定义的函数,以满足项目要求。请帮忙。


<details>
<summary>英文:</summary>

First Attempt - 

CREATE FUNCTION rental_quarter (rental_date TIMESTAMP)
RETURNS VARCHAR(7)
BEGIN
IF MONTH(rental_date) BETWEEN 1 AND 3 THEN RETURN YEAR(rental_date)+ ”Q1”;
ELSE IF MONTH(rental_date) BETWEEN 4 AND 6 THEN RETURN YEAR(rental_date)+ ”Q2” ;
ELSE IF MONTH(rental_date) BETWEEN 7 AND 9 THEN RETURN YEAR(rental_date)+ ”Q3”;
ELSE IF MONTH(rental_date) BETWEEN 10 AND 12 THEN RETURN YEAR(rental_date)+ ”Q4”;
ELSE RETURN NULL ;
END IF;
END;



Latest try [Code and Error](https://i.stack.imgur.com/Bw3xX.png)

[EDIT Suggested fix error #1][1] 


I am new to sql and have been stuck on this a few hours . I am trying to create a rental_quarter(rental_date) function that turns the time stamped date format (without timezone) into year + quarter. I have no Idea what I am doing wrong. I have tried using AS, DECLARE, SET AS. I&#39;ve done every variation of semicolons. It has to be a user defined function to meet project requirements. Please help. 


  [1]: https://i.stack.imgur.com/AdodW.png

</details>


# 答案1
**得分**: 0

已经有一个用于此目的的函数,to_char(),您不必自己编写一个:

SELECT to_char('2023-08-01'::timestamp, 'YYYY"Q"Q');

结果:2023Q3

<details>
<summary>英文:</summary>

There is already a function for this, to_char(), you don&#39;t have write one yourself:

    SELECT to_char(&#39;2023-08-01&#39;::timestamp, &#39;YYYY&quot;Q&quot;Q&#39;);

Result: 2023Q3

</details>



# 答案2
**得分**: -1

这是我认为需要最少更改的版本:

```sql
CREATE FUNCTION rental_quarter (rental_date TIMESTAMP)
RETURNS VARCHAR(7) AS
$BODY$
BEGIN 
 IF MONTH(rental_date) BETWEEN 1 AND 3 THEN RETURN YEAR(rental_date) || 'Q1';
 ELSIF MONTH(rental_date) BETWEEN 4 AND 6 THEN RETURN YEAR(rental_date) || 'Q2';  
 ELSIF MONTH(rental_date) BETWEEN 7 AND 9 THEN RETURN YEAR(rental_date) || 'Q3';
 ELSIF MONTH(rental_date) BETWEEN 10 AND 12 THEN RETURN YEAR(rental_date) || 'Q4';
 ELSE RETURN NULL ;
 END IF;
END;
$BODY$
LANGUAGE PLPGSQL;
英文:

This is with minimum changes I think:

CREATE FUNCTION rental_quarter (rental_date TIMESTAMP)
RETURNS VARCHAR(7) AS
$BODY$
BEGIN 
 IF MONTH(rental_date) BETWEEN 1 AND 3 THEN RETURN YEAR(rental_date)+ ”Q1”;
 ELSIF MONTH(rental_date) BETWEEN 4 AND 6 THEN RETURN YEAR(rental_date)+ ”Q2”;  
 ELSIF MONTH(rental_date) BETWEEN 7 AND 9 THEN RETURN YEAR(rental_date)+ ”Q3”;
 ELSIF MONTH(rental_date) BETWEEN 10 AND 12 THEN RETURN YEAR(rental_date)+ ”Q4”;
 ELSE RETURN NULL ;
 END IF;
END;
$BODY$
LANGUAGE PLPGSQL;

huangapple
  • 本文由 发表于 2023年7月3日 04:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600595.html
匿名

发表评论

匿名网友

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

确定