SQL Server’da Tarih Bilgisinden Çeşitli Tarih Bilgileri Elde Etmek

Herkese merhaba,

Bu yazıda SQL Server’da tarih bilgisinden çeşitli tarih bilgilerinin nasıl elde edileceği hakkında bilgi vermeye çalışacağım.

SQL Server’da elinizde olan tarih bilgisinin farklı varyasyonlarını görmek ya da kullanmak isteyebilirsiniz.

Aşağıdaki kodu kullanarak siz de bu bilgilerden bazılarını kullanabilirsiniz.

DECLARE @Date DATE;
SET @Date = GETDATE()
SELECT @Date AS [Date],
       YEAR(@Date) * 10000 + MONTH(@Date) * 100 + DAY(@Date) AS [DateKey],
       DAY(@Date) AS [Day Number],
       DATENAME(DAY, @Date) AS [Day],
       CAST(DATEPART(dy, @Date) AS NVARCHAR(5)) AS [Day of Year],
       DATEPART(DAY, @Date) AS [Day of Year Number],
       DATENAME(WEEKDAY, @Date) AS [Day of Week],
       DATEPART(WEEKDAY, @Date) AS [Day of Week Number],
       DATENAME(WEEK, @Date) AS [Week of Year],
       DATENAME(MONTH, @Date) AS [Month],
       SUBSTRING(DATENAME(MONTH, @Date), 1, 3) AS [Short Month],
       N'Q' + DATENAME(QUARTER, @Date) AS [Quarter],
       N'H' + CASE
                  WHEN DATEPART(MONTH, @Date) < 7 THEN
                      N'1'
                  ELSE
                      N'2'
              END AS [Half of Year],
       CAST(DATENAME(YEAR, @Date) + N'-' + DATENAME(MONTH, @Date) + N'-01' AS DATE) AS [Beginning of Month],
       CASE
           WHEN MONTH(@Date)
                BETWEEN 1 AND 3 THEN
               CAST(DATENAME(YEAR, @Date) + '-01-01' AS DATE)
           WHEN MONTH(@Date)
                BETWEEN 4 AND 6 THEN
               CAST(DATENAME(YEAR, @Date) + '-04-01' AS DATE)
           WHEN MONTH(@Date)
                BETWEEN 7 AND 9 THEN
               CAST(DATENAME(YEAR, @Date) + '-07-01' AS DATE)
           WHEN MONTH(@Date)
                BETWEEN 10 AND 12 THEN
               CAST(DATENAME(YEAR, @Date) + '-10-01' AS DATE)
       END AS [Beginning of Quarter],
       CASE
           WHEN DATEPART(MONTH, @Date) < 7 THEN
               CAST(DATENAME(YEAR, @Date) + '-01-01' AS DATE)
           ELSE
               CAST(DATENAME(YEAR, @Date) + '-07-01' AS DATE)
       END AS [Beginning of Half of Year],
       CAST(DATENAME(YEAR, @Date) + N'-01-01' AS DATE) AS [Beginning of Year],
       N'Beginning of Month ' + DATENAME(MONTH, @Date) + N'-' + DATENAME(YEAR, @Date) AS [Beginning of Month Label],
       N'BOM ' + SUBSTRING(DATENAME(MONTH, @Date), 1, 3) + N'-' + DATENAME(YEAR, @Date) AS [Beginning of Month Label Short],
       CASE
           WHEN MONTH(@Date)
                BETWEEN 1 AND 3 THEN
               N'Beginning Of Quarter ' + DATENAME(YEAR, @Date) + N'-Q1'
           WHEN MONTH(@Date)
                BETWEEN 4 AND 6 THEN
               N'Beginning Of Quarter ' + DATENAME(YEAR, @Date) + N'-Q2'
           WHEN MONTH(@Date)
                BETWEEN 7 AND 9 THEN
               N'Beginning Of Quarter ' + DATENAME(YEAR, @Date) + N'-Q3'
           WHEN MONTH(@Date)
                BETWEEN 10 AND 12 THEN
               N'Beginning Of Quarter ' + DATENAME(YEAR, @Date) + N'-Q4'
       END AS [Beginning of Quarter Label],
       CASE
           WHEN MONTH(@Date)
                BETWEEN 1 AND 3 THEN
               N'BOQ ' + DATENAME(YEAR, @Date) + N'-Q1'
           WHEN MONTH(@Date)
                BETWEEN 4 AND 6 THEN
               N'BOQ ' + DATENAME(YEAR, @Date) + N'-Q2'
           WHEN MONTH(@Date)
                BETWEEN 7 AND 9 THEN
               N'BOQ ' + DATENAME(YEAR, @Date) + N'-Q3'
           WHEN MONTH(@Date)
                BETWEEN 10 AND 12 THEN
               N'BOQ ' + DATENAME(YEAR, @Date) + N'-Q4'
       END AS [Beginning of Quarter Label Short],
       CASE
           WHEN DATEPART(MONTH, @Date) < 7 THEN
               N'Beginning of Half Year ' + DATENAME(YEAR, @Date) + N'-H1'
           ELSE
               N'Beginning of Half Year ' + DATENAME(YEAR, @Date) + N'-H2'
       END AS [Beginning of Half Year Label],
       CASE
           WHEN DATEPART(MONTH, @Date) < 7 THEN
               N'BOH ' + DATENAME(YEAR, @Date) + N'-H1'
           ELSE
               N'BOH ' + DATENAME(YEAR, @Date) + N'-H2'
       END AS [Beginning of Half Year Label Short],
       N'Beginning of Year ' + DATENAME(YEAR, @Date) AS [Beginning of Year Label],
       N'BOY ' + DATENAME(YEAR, @Date) AS [Beginning of Year Label Short],
       DATENAME(MONTH, @Date) + N' ' + DATENAME(DAY, @Date) + N', ' + DATENAME(YEAR, @Date) AS [Calendar Day Label],
       SUBSTRING(DATENAME(MONTH, @Date), 1, 3) + N' ' + DATENAME(DAY, @Date) + N', ' + DATENAME(YEAR, @Date) AS [Calendar Day Label Short],
       DATEPART(WEEK, @Date) AS [Calendar Week Number],
       N'CY' + DATENAME(YEAR, @Date) + '-W' + RIGHT(N'00' + DATENAME(WEEK, @Date), 2) AS [Calendar Week Label],
       MONTH(@Date) AS [Calendar Month Number],
       N'CY' + DATENAME(YEAR, @Date) + N'-' + SUBSTRING(DATENAME(MONTH, @Date), 1, 3) AS [Calendar Month Label],
       SUBSTRING(DATENAME(MONTH, @Date), 1, 3) + N'-' + DATENAME(YEAR, @Date) AS [Calendar Month Year Label],
       DATEPART(QUARTER, @Date) AS [Calendar Quarter Number],
       N'CY' + DATENAME(YEAR, @Date) + N'-Q' + DATENAME(QUARTER, @Date) AS [Calendar Quarter Label],
       N'Q' + DATENAME(QUARTER, @Date) + N'-' + DATENAME(YEAR, @Date) AS [Calendar Quarter Year Label],
       CASE
           WHEN DATEPART(MONTH, @Date) < 7 THEN
               1
           ELSE
               2
       END AS [Calendar Half of Year Number],
       N'CY' + DATENAME(YEAR, @Date) + N'-H' + CASE
                                                   WHEN DATEPART(MONTH, @Date) < 7 THEN
                                                       N'1'
                                                   ELSE
                                                       N'2'
                                               END AS [Calendar Half of Year Label],
       N'H' + CASE
                  WHEN DATEPART(MONTH, @Date) < 7 THEN
                      N'1'
                  ELSE
                      N'2'
              END + N'-' + DATENAME(YEAR, @Date) AS [Calendar Year Half of Year Label],
       YEAR(@Date) AS [Calendar Year],
       N'CY' + DATENAME(YEAR, @Date) AS [Calendar Year Label],
       CASE
           WHEN MONTH(@Date) > 6 THEN
               MONTH(@Date) - 6
           ELSE
               MONTH(@Date) + 6
       END AS [Fiscal Month Number],
       CAST(N'FY' + CAST(CASE
                             WHEN MONTH(@Date) > 6 THEN
                                 YEAR(@Date) + 1
                             ELSE
                                 YEAR(@Date)
                         END AS NVARCHAR(4)) + N'-' + SUBSTRING(DATENAME(MONTH, @Date), 1, 3) AS NVARCHAR(20)) AS [Fiscal Month Label],
       CASE
           WHEN MONTH(@Date) > 6 THEN
               DATEPART(QUARTER, @Date) - 2
           ELSE
               DATEPART(QUARTER, @Date) + 2
       END AS [Fiscal Quarter Number],
       N'FY' + CAST(CASE
                        WHEN MONTH(@Date) > 6 THEN
                            YEAR(@Date) + 1
                        ELSE
                            YEAR(@Date)
                    END AS NVARCHAR(4)) + N'-Q' + CASE
                                                      WHEN MONTH(@Date) > 6 THEN
                                                          CAST(DATEPART(QUARTER, @Date) - 2 AS NVARCHAR(2))
                                                      ELSE
                                                          CAST(DATEPART(QUARTER, @Date) + 2 AS NVARCHAR(2))
                                                  END AS [Fiscal Quarter Label],
       CASE
           WHEN MONTH(@Date) > 6 THEN
               1
           ELSE
               2
       END AS [Fiscal Half of Year Number],
       N'FY' + CAST(CASE
                        WHEN MONTH(@Date) > 6 THEN
                            YEAR(@Date) + 1
                        ELSE
                            YEAR(@Date)
                    END AS NVARCHAR(4)) + N'-H' + CASE
                                                      WHEN MONTH(@Date) > 6 THEN
                                                          N'1'
                                                      ELSE
                                                          N'2'
                                                  END AS [Fiscal Half of Year Label],
       CASE
           WHEN MONTH(@Date) > 6 THEN
               YEAR(@Date) + 1
           ELSE
               YEAR(@Date)
       END AS [Fiscal Year],
       N'FY' + CAST(CASE
                        WHEN MONTH(@Date) > 6 THEN
                            YEAR(@Date) + 1
                        ELSE
                            YEAR(@Date)
                    END AS NVARCHAR(4)) AS [Fiscal Year Label],
       YEAR(@Date) * 10000 + MONTH(@Date) * 100 + DAY(@Date) AS [Date Key],
       YEAR(@Date) * 100 + DATEPART(WEEK, @Date) AS [Year Week Key],
       YEAR(@Date) * 100 + MONTH(@Date) AS [Year Month Key],
       YEAR(@Date) * 10 + DATEPART(QUARTER, @Date) AS [Year Quarter Key],
       YEAR(@Date) * 10 + CASE
                              WHEN DATEPART(MONTH, @Date) < 7 THEN
                                  1
                              ELSE
                                  2
                          END AS [Year Half of Year Key],
       YEAR(@Date) AS [Year Key],
       CASE
           WHEN MONTH(@Date) > 6 THEN
       (YEAR(@Date) + 1) * 100 + MONTH(@Date)
           ELSE
               YEAR(@Date) * 100 + MONTH(@Date)
       END AS [Fiscal Year Month Key],
       (YEAR(@Date) * 10000) + (MONTH(@Date) * 100) + 1 AS [Beginning of Month Key],
       CASE
           WHEN MONTH(@Date)
                BETWEEN 1 AND 3 THEN
       (YEAR(@Date) * 10000) + 0101
           WHEN MONTH(@Date)
                BETWEEN 4 AND 6 THEN
       (YEAR(@Date) * 10000) + 0401
           WHEN MONTH(@Date)
                BETWEEN 7 AND 9 THEN
       (YEAR(@Date) * 10000) + 0701
           WHEN MONTH(@Date)
                BETWEEN 10 AND 12 THEN
       (YEAR(@Date) * 10000) + 1001
       END AS [Beginning of Quarter Key],
       CASE
           WHEN DATEPART(MONTH, @Date) < 7 THEN
       (YEAR(@Date) * 10000) + 0101
           ELSE
       (YEAR(@Date) * 10000) + 0701
       END AS [Beginning of Half of Year Key],
       (YEAR(@Date) * 10000) + 0101 AS [Beginning of Year Key],
       CASE
           WHEN MONTH(@Date) > 6 THEN
       ((YEAR(@Date) + 1) * 10) + DATEPART(QUARTER, @Date) - 2
           ELSE
       (YEAR(@Date) * 10) + DATEPART(QUARTER, @Date) + 2
       END AS [Fiscal Year Quarter Key],
       CASE
           WHEN MONTH(@Date) > 6 THEN
       ((YEAR(@Date) + 1) * 10) + 1
           ELSE
       (YEAR(@Date) * 10) + 2
       END AS [Fiscal Year Half of Year Key],
       DATEPART(ISO_WEEK, @Date) AS [ISO Week Number];

Yukarıdaki sorguyu çalıştırınca aşağıdakine benzer bir sonuç göreceksiniz.

SQL Server'da Tarih Bilgisinden Çeşitli Tarih Bilgileri Elde Etmek

Görüldüğü üzere çeşitli tarih bilgilerini elde etmiş olduk.

Herkese çalışma hayatında ve yaşamında başarılar kolaylıklar.

182 Kez Okundu

Yavuz Selim Kart

Yazılım ve veri tabanında bildiklerimi anlatmaya gayret ediyorum. Halen birçok programlama dili üzerinde araştırmalar yaparak kendimi geliştiriyorum. Bunlar haricinde Grafik Tasarım ve Wordpress konularına da ilgim bulunmakta. SEO ve Sosyal medya yönetimleri hakkında da bilgim mevcuttur. Kısaca çok çalışmayı seven azimli biriyim.

You may also like...

Yazı kopyalamak yasaktır!