CREATE FUNCTION dbo.GetThirdOfYear(@d date)
RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
(
SELECT Third = DATEADD(MONTH,
4*((MONTH(@d)-1)/4),
DATEFROMPARTS(YEAR(@d),1,1))
);
示例用法,给定一个名为Events的表,其中列名为EventDate:
SELECT e.EventDate, f.Third
FROM dbo.Events AS e
CROSS APPLY dbo.GetThirdOfYear(e.EventDate) AS f;
1条答案
按热度按时间qxsslcnc1#
我们可以通过将4个月乘以(月-1)/4加到年初来计算日期。
示例用法,给定一个名为
Events
的表,其中列名为EventDate
: