SQL Server’da Dinamik Tarih Oluşturmak

Herkese merhaba,

Bu kısımda SQL Server’da dinamik tarih oluşturma kodları olacak.

WITH DinamikTarihOlusturucu(BaseYearMinus, BaseYearPlus)
AS
(
	SELECT DATEFROMPARTS(YEAR(GETDATE()), 1, 1) BaseYearMinus, DATEFROMPARTS(YEAR(GETDATE()), 1, 1) BaseYearPlus
	UNION ALL
	SELECT DATEADD(YEAR, -1, BaseYearMinus) BaseYearMinus, DATEADD(YEAR, 1, BaseYearPlus) BaseYearPlus FROM DinamikTarihOlusturucu
	WHERE (DATEDIFF(YEAR, BaseYearMinus, BaseYearPlus)/2) < (1 /*  Buraya yazılan rakam kadar var olan yıldan aşağı ve yukarı yılları getirir  */)
)
SELECT UnPvt.RangeYear
FROM
(
	SELECT YEAR(BaseYearMinus) YearMinus, YEAR(BaseYearPlus) YearPlus FROM DinamikTarihOlusturucu
) Tbl
UNPIVOT (RangeYear FOR ReleaseYear IN (YearMinus, YearPlus)) UnPvt
GROUP BY RangeYear
ORDER BY RangeYear;
SQL Server Eğitimleri Kanalına Abone Olun

Şunlar makaleler de hoşunuza gidebilir