How to Get Quarter from Date in SQL Server

In this comprehensive article, I’ll share four different methods that I have successfully used in production environments, complete with real-world examples from actual implementations. Each method has its place depending on your specific requirements, performance needs, and business logic.

How to Get Quarter from Date in SQL Server

Before diving into the technical methods, let me explain why quarter extraction is crucial for American businesses:

  • Financial Reporting: Publicly traded companies must report quarterly earnings
  • Sales Analytics: Tracking quarterly performance against targets and forecasts
  • Seasonal Analysis: Understanding business patterns across quarters
  • Budget Planning: Most enterprises plan budgets on a quarterly cycle
  • Compliance Requirements: Many regulations require quarterly reporting

Method 1: Using DATEPART Function

The DATEPART() Function is my go-to method for extracting quarters because it’s straightforward, widely understood, and performs well with proper indexing strategies.

Basic Syntax

SELECT 
    sale_Date,
    DATEPART(QUARTER, sale_Date) AS Quarter,
    DATEPART(YEAR, sale_Date) AS Year
FROM Sales;

After executing the above query, I got the expected output as shown in the screenshot below.

How to Get Quarter from Date in SQL Server

Practical Example: Quarterly Sales Analysis

Here’s how I implemented quarterly analysis for a retail client:

-- Quarterly sales summary for US retail chain
SELECT 
    DATEPART(YEAR, o.OrderDate) AS SalesYear,
    DATEPART(QUARTER, o.OrderDate) AS Quarter,
    COUNT(*) AS TotalOrders,
    SUM(o.TotalAmount) AS QuarterlyRevenue,
    AVG(o.TotalAmount) AS AverageOrderValue,
    COUNT(DISTINCT o.CustomerID) AS UniqueCustomers
FROM Sales.Orders o
INNER JOIN Customers.CustomerInfo c ON o.CustomerID = c.CustomerID
WHERE o.OrderDate >= '2023-01-01'
    AND c.State IN ('California', 'Texas', 'New York', 'Florida')
GROUP BY 
    DATEPART(YEAR, o.OrderDate),
    DATEPART(QUARTER, o.OrderDate)
ORDER BY SalesYear, Quarter;

After executing the above query, I got the expected output as shown in the screenshot below.

how to get quarter from date in sql server 2008

Method 2: Using Mathematical Calculations

Some businesses use fiscal years or custom quarter definitions. I’ve implemented mathematical approaches for those clients.

Standard Calendar Quarter Calculation

-- Mathematical approach using MONTH function
SELECT 
    OrderDate,
    YEAR(OrderDate) AS Year,
    (MONTH(OrderDate) - 1) / 3 + 1 AS Quarter,
    CASE 
        WHEN MONTH(OrderDate) IN (1, 2, 3) THEN 'Q1'
        WHEN MONTH(OrderDate) IN (4, 5, 6) THEN 'Q2'
        WHEN MONTH(OrderDate) IN (7, 8, 9) THEN 'Q3'
        WHEN MONTH(OrderDate) IN (10, 11, 12) THEN 'Q4'
    END AS QuarterName
FROM Sales.Orders;

After executing the above query, I got the expected output as shown in the screenshot below.

how to get quarter and year from date in sql server

Fiscal Year Quarter Implementation

Here’s how I implemented fiscal year quarters for a manufacturing client using the below query:

-- Fiscal year quarters (October 1st year start)
DECLARE @FiscalYearStart INT = 10; -- October

SELECT 
    OrderDate,
    -- Fiscal Year calculation
    CASE 
        WHEN MONTH(OrderDate) >= @FiscalYearStart 
        THEN YEAR(OrderDate) + 1
        ELSE YEAR(OrderDate)
    END AS FiscalYear,
    -- Fiscal Quarter calculation
    CASE 
        WHEN MONTH(OrderDate) IN (10, 11, 12) THEN 1 -- Q1: Oct-Dec
        WHEN MONTH(OrderDate) IN (1, 2, 3) THEN 2    -- Q2: Jan-Mar
        WHEN MONTH(OrderDate) IN (4, 5, 6) THEN 3    -- Q3: Apr-Jun
        WHEN MONTH(OrderDate) IN (7, 8, 9) THEN 4    -- Q4: Jul-Sep
    END AS FiscalQuarter,
    TotalAmount
FROM Sales.Orders
WHERE OrderDate >= '2023-01-01'
ORDER BY OrderDate;

After executing the above query, I got the expected output as shown in the screenshot below.

how to get quarter from date in sql

Method 3: Using the FORMAT Function

The FORMAT() Function provides excellent flexibility for creating quarter displays, especially useful for reports and dashboards.

Basic Quarter Formatting

-- Using FORMAT for quarter display
SELECT 
    OrderDate,
    FORMAT(OrderDate, 'yyyy') + '-Q' + CAST(DATEPART(QUARTER, OrderDate) AS VARCHAR(1)) AS QuarterDisplay,
    CONCAT('Q', DATEPART(QUARTER, OrderDate), ' ', YEAR(OrderDate)) AS AlternateDisplay,
    TotalAmount
FROM Sales.Orders
WHERE OrderDate >= '2023-01-01'
ORDER BY OrderDate;

After executing the above query, I got the expected output as shown in the screenshot below.

get quarter from date sql server

Method 4: Using CASE Statements – For Complex Logic

When business rules require complex quarter logic, I use CASE statements for maximum flexibility and readability.

Here’s how I implemented retail-specific quarters for a retailer using the below query:

-- Retail calendar quarters (different from standard quarters)
SELECT 
    OrderDate,
    ProductCategory,
    CASE 
        -- Spring Season: Feb-Apr
        WHEN MONTH(OrderDate) IN (2, 3, 4) THEN 'Spring Quarter'
 -- Spring Season: Feb-Apr
        WHEN MONTH(OrderDate) IN (2, 3, 4) THEN 'Spring Quarter'
        -- Summer Season: May-Jul
        WHEN MONTH(OrderDate) IN (5, 6, 7) THEN 'Summer Quarter'
        -- Fall/Back-to-School: Aug-Oct
        WHEN MONTH(OrderDate) IN (8, 9, 10) THEN 'Fall Quarter'
        -- Holiday/Winter: Nov-Jan
        WHEN MONTH(OrderDate) IN (11, 12, 1) THEN 'Holiday Quarter'
    END AS RetailSeason,
    CASE 
        WHEN MONTH(OrderDate) IN (2, 3, 4) THEN 1
        WHEN MONTH(OrderDate) IN (5, 6, 7) THEN 2
        WHEN MONTH(OrderDate) IN (8, 9, 10) THEN 3
        WHEN MONTH(OrderDate) IN (11, 12, 1) THEN 4
    END AS RetailQuarterNumber,
    SUM(TotalAmount) AS SeasonRevenue,
    COUNT(*) AS SeasonOrders
FROM Sales.Orders o
INNER JOIN Products.ProductInfo p ON o.ProductID = p.ProductID
WHERE o.OrderDate >= '2023-01-01'
    AND p.Category IN ('Apparel', 'Footwear', 'Accessories')
GROUP BY 
    OrderDate,
    ProductCategory,
    CASE 
        WHEN MONTH(OrderDate) IN (2, 3, 4) THEN 'Spring Quarter'
        WHEN MONTH(OrderDate) IN (5, 6, 7) THEN 'Summer Quarter'
        WHEN MONTH(OrderDate) IN (8, 9, 10) THEN 'Fall Quarter'
        WHEN MONTH(OrderDate) IN (11, 12, 1) THEN 'Holiday Quarter'
    END,
    CASE 
        WHEN MONTH(OrderDate) IN (2, 3, 4) THEN 1
        WHEN MONTH(OrderDate) IN (5, 6, 7) THEN 2
        WHEN MONTH(OrderDate) IN (8, 9, 10) THEN 3
        WHEN MONTH(OrderDate) IN (11, 12, 1) THEN 4
    END
ORDER BY OrderDate;

Conclusion

Knowing quarter extraction techniques is essential for any SQL Server professional working with business analytics and reporting.

Throughout this comprehensive article, I’ve shared four proven methods that I use regularly in production environments:

Method Recommendation Summary:

MethodBest Use CasePerformanceComplexityMaintenance
DATEPART()Standard calendar quartersExcellentLowMinimal
Mathematical CalculationsCustom quarter definitionsGoodMediumLow
FORMAT FunctionDisplay and reportingFairLowLow
CASE StatementsComplex business logicGoodHighMedium

Key Takeaways:

  • Always consider performance implications when choosing your approach
  • Use date range filtering instead of functions in WHERE clauses
  • Create computed columns for frequently queried quarter information
  • Implement proper indexing strategies on date columns
  • Maintain consistency across your organization’s reporting standards

You may also like the following articles