英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论