将字符串连接并转化为日期。

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

Concat and make a string into a date

问题

我有一个表格,其中月份和年份是分开的,我想把它们组合在一起,并将后端格式转换为日期。我尝试使用cast/converting,但出现了一个错误,错误信息为“Conversion failed when converting date and/or time from character string”。

这是数据:

会计期间 | 会计年度
1            | 2023
2            | 2024
3            | 2023
10          | 2024

期望的输出,或者任何日期输出只要是日期的格式:

01/01/2023
02/01/2024
03/01/2023
10/01/2024

当我尝试输出时,我试图将结果转换,但由于字符串而出现错误。以下是我的代码:

select cast(concat(tb.AccountingPeriod,tb.AccountingYear) as date) as month_year_date  
from  ttbl_a  tb
英文:

I have a table where the month and year are separate, and I want to put them together and make the backend format to date. I tried cast/converting but I get an error of "Conversion failed when converting date and/or time from character string"

This is the data

Accountingperiod | Accounting year
1                    2023
2                    2024
3                    2023
10                   2024

desired out put or any date output as long as its date

01/01/2023
02/01/2024
03/01/2023
10/01/2024

and when I output I try to cast the result but I get an error because of the string . Here is my code

select cast(concat(tb.AccountingPeriod,tb.AccountingYear) as date) as month_year_date  
from  ttbl_a  tb 

答案1

得分: 0

感谢 @siggemannen,我使用了以下代码并得到了我想要的结果。

select DATEFROMPARTS (AccountingYear, AccountingPeriod, 1) as yy_mm_date from ttbl_a

输出:

yy_mm_date
2023-01-01
2024-02-01
2023-03-01
2024-10-01
英文:

Thanks @siggemannen, I used the below and it gave me what I want.

select DATEFROMPARTS (AccountingYear,AccountingPeriod,1) as yy_mm_date from ttbl_a

output

yy_mm_date
2023 -01-01  
2024 -02-01 
2023 -03-01 
2024 -10-01 

huangapple
  • 本文由 发表于 2023年7月14日 05:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683219.html
匿名

发表评论

匿名网友

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

确定