从日期生成月份和年份并自动存储在 table 中

Generate month and year from date and store automatically in table

我在 sql 服务器 2005 上工作,我有一个数据类型为 datetime 的列 'Date',我想分别提取月份和年份并将其存储在同一 [=16= 的两个单独的列中] 为 'Date'。我如何做到这一点并使其在每次输入新条目时自动发生,而无需手动输入月份和日期。

您可以使用计算列或触发器。 我建议使用计算列。它非常容易实施和维护。

要获得 yearmonth 使用 YEARMONTH 函数

Computed Columns in SQL Server

DateTime functions in SQL Server