获取仅基于月份和年份组合的第一个和最后一个日期

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

Get first and last dates based on only Month and Year combination

问题

我尝试了:

DATE_FORMAT(STR_TO_DATE(CONCAT('01 ',delivery),'%d %b %Y'),'%Y-%m-%d') as startdate

DATE_FORMAT(LAST_DAY(STR_TO_DATE(delivery,'%b %Y')),'%Y-%m-%d') as enddate

但是它不起作用。我得到一个错误:Sep 2021对于STR_TO_DATE来说不是有效的。

有人可以提供一个正确、简单和高效的实现方式吗?

英文:

We have a couple of staging tables for Data in mysql db that we read from a input file. In the first table, the data is dumped as is. This data is then processed and grouped in certain ways and result is inserted in the second staging table.
One of the fields has original values in 'MMM YYYY' format, e.g. 'Sep 2021', 'Jul 2022' etc. and during processing we need to calculate the first and last date of this combination and resultant fields will be called startdate and enddate, in format YYYY-MM-dd so Sep 2021 should result in 2021-09-01 as startdate and 2021-09-30 as enddate respectively.

I had tried:

DATE_FORMAT(STR_TO_DATE(CONCAT('01 ',delivery),'%d %b %Y'),'%m/%d/%y') as startdate

and

DATE_FORMAT(LAST_DAY(STR_TO_DATE(delivery,'%b %Y')),'%m/%d/%y') as enddate

But it is not working. I get an error: Sep 2021 is not valid for STR_TO_DATE

Can someone provide the the correct, simplest and most efficient way of achieving this?

答案1

得分: 0

你收到的错误消息表明STR_TO_DATE()函数无法解析输入日期字符串的格式为'MMM YYYY'。这是因为函数中的'%b'格式说明符期望月份缩写为全大写字母,但你的输入字符串的首字母是大写的。

为了解决这个问题,你可以在应用STR_TO_DATE()函数之前将月份缩写的第一个字母转换为大写。以下是一个示例查询,它执行这个操作并按照要求计算起始日期和结束日期:

SELECT delivery, 
       DATE_FORMAT(STR_TO_DATE(CONCAT('01 ', UPPER(LEFT(delivery, 3)), SUBSTRING(delivery, 4)), '%d %b %Y'), '%Y-%m-%d') AS startdate, 
       DATE_FORMAT(LAST_DAY(STR_TO_DATE(CONCAT('01 ', UPPER(LEFT(delivery, 3)), SUBSTRING(delivery, 4)), '%d %b %Y')), '%Y-%m-%d') AS enddate
FROM staging_table_2;

在这个查询中,UPPER()函数用于将月份缩写的第一个字母转换为大写,LEFT()和SUBSTRING()函数用于从'MMM YYYY'格式中提取月份缩写和年份。

生成的日期字符串使用'%Y-%m-%d'格式说明符进行格式化,该格式提供的日期格式为'YYYY-MM-DD'。这种格式等同于ISO 8601标准,适合在数据库中存储日期。

英文:

The error message you're getting suggests that the STR_TO_DATE() function is not able to parse the input date string in the format 'MMM YYYY'. This is because the '%b' format specifier in the function expects the month abbreviation to be in all uppercase letters, but your input strings have the first letter capitalized.

To fix this, you can convert the first letter of the month abbreviation to uppercase before applying the STR_TO_DATE() function. Here's an example query that does this and calculates the start and end dates as requested:

    SELECT delivery, 
       DATE_FORMAT(STR_TO_DATE(CONCAT('01 ', UPPER(LEFT(delivery, 3)), SUBSTRING(delivery, 4)), '%d %b %Y'), '%Y-%m-%d') AS startdate, 
       DATE_FORMAT(LAST_DAY(STR_TO_DATE(CONCAT('01 ', UPPER(LEFT(delivery, 3)), SUBSTRING(delivery, 4)), '%d %b %Y')), '%Y-%m-%d') AS enddate
FROM staging_table_2;

In this query, the UPPER() function is used to convert the first letter of the month abbreviation to uppercase, and the LEFT() and SUBSTRING() functions are used to extract the month abbreviation and year from the 'MMM YYYY' format.

The resulting date strings are formatted using the %Y-%m-%d format specifier, which gives dates in the format 'YYYY-MM-DD'. This format is equivalent to the ISO 8601 standard and is a good choice for storing dates in a database.

答案2

得分: 0

你的解决方案几乎正确,请尝试这样做:

这是开始日期:

SELECT DATE_FORMAT(STR_TO_DATE(CONCAT('01 ', 'Sep 2021'),'%d %b %Y'),'YYYY-MM-DD') as startdate

对于end date,我们通过STR_TO_DATE转换日期,然后对其应用LAST_DAY

SELECT DATE_FORMAT(LAST_DAY(STR_TO_DATE(CONCAT('01 ', 'Sep 2021'),'%d %b %Y')),'YYYY-MM-DD') as enddate
英文:

Your solution almost correct, try this :

This is for start date :

SELECT DATE_FORMAT(STR_TO_DATE(CONCAT('01 ', 'Sep 2021'),'%d %b %Y'),'%Y-%m-%d') as startdate

This for end date, we take the converted date by STR_TO_DATE then apply LAST_DAY on it

SELECT DATE_FORMAT(LAST_DAY(STR_TO_DATE(CONCAT('01 ', 'Sep 2021'),'%d %b %Y')),'%Y-%m-%d') as enddate

Demo here

huangapple
  • 本文由 发表于 2023年4月19日 19:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053791.html
匿名

发表评论

匿名网友

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

确定