简体   繁体   English

在 SQL 服务器中存储季度/年

[英]Store Quarter/Year in SQL Server

I have a table which needs to store the quarter and year, and I need to know which is the best way to do this.我有一张需要存储季度和年份的表,我需要知道哪种方法是最好的方法。 I found this answer from 10 years ago on SO: Best way to store quarter and year in SQL Server .我从 10 年前在 SO 上找到了这个答案: Best way to store quarter and year in SQL Server However, there are two suggestions given--one is storing quarter and year in separate columns and making them integers, the other being storing as a datetime and using the first day of the month for the day (ie, 1/1/2021, 4/1/2021, etc.).但是,给出了两个建议 - 一个是将季度和年份存储在单独的列中并将它们设为整数,另一个是存储为日期时间并使用该月的第一天作为当天(即 2021 年 1 月 1 日, 2021 年 4 月 1 日等)。

Considering this answer is 10 years old and there could be better ways now for storing this data, what is the best method?考虑到这个答案已有 10 年历史,现在可能有更好的方法来存储这些数据,最好的方法是什么?

FYI, this data will not be used for calculation purposes, but will probably be searched on.仅供参考,此数据不会用于计算目的,但可能会被搜索。

Thanks!谢谢!

I recommend always storing date related data as the datetime data type.我建议始终将与日期相关的数据存储为日期时间数据类型。

Storing them separately is the worst possible approach, searching becomes very difficult.将它们分开存储是最糟糕的方法,搜索变得非常困难。 Try writing the query returning all quarters between 3Q2019 and 1Q2021 when your year and quarter are separate.当您的年份和季度分开时,尝试编写返回 3Q2019 和 1Q2021 之间所有季度的查询。

Breaking it into separate parts puts the responsibility on the developer to handle the year boundary appropriately, which many do not.将其分解为单独的部分使开发人员有责任适当地处理年份边界,而许多人没有这样做。

DateTime data type also includes validation (Q5 2020 would throw an error) to prevent data errors. DateTime 数据类型还包括验证(Q5 2020 将引发错误)以防止数据错误。

Use the right tool for the job.为工作使用正确的工具。 DateTime data should always be stored in a DateTime datatype. DateTime 数据应始终存储在 DateTime 数据类型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM