Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:02:20+00:00 2026-06-07T05:02:20+00:00

SELECT DATEPART(YEAR, PURCHASE_DATE) AS TRANSACTION_YEAR, DATEPART(MONTH, PURCHASE_DATE) AS TRANSACTION_MONTH, CASE WHEN DATEPART(YEAR, DUE_DATE) >=

  • 0
SELECT DATEPART(YEAR, PURCHASE_DATE) AS TRANSACTION_YEAR, DATEPART(MONTH, PURCHASE_DATE) AS TRANSACTION_MONTH,
CASE WHEN DATEPART(YEAR, DUE_DATE) >= DATEPART(YEAR, PURCHASE_DATE) THEN DATEPART(YEAR, DUE_DATE)
ELSE DATEPART(YEAR, PURCHASE_DATE) END AS RELEASE_YEAR, 
CASE WHEN (DATEPART(YEAR, DUE_DATE) = DATEPART(YEAR, PURCHASE_DATE) AND DATEPART(MONTH, DUE_DATE) >= DATEPART(MONTH, PURCHASE_DATE)) THEN DATEPART(MONTH, DUE_DATE)
WHEN DATEPART(YEAR, DUE_DATE) > DATEPART(YEAR, PURCHASE_DATE) THEN DATEPART(MONTH, DUE_DATE)
ELSE
DATEPART(MONTH, PURCHASE_DATE) END AS RELEASE_MONTH,
CAST(YEAR(CASE WHEN DATEPART(YEAR, DUE_DATE) >= DATEPART(YEAR, PURCHASE_DATE) THEN DATEPART(YEAR, DUE_DATE)
ELSE DATEPART(YEAR, PURCHASE_DATE) END) AS VARCHAR(4)) + RIGHT('0'+CAST(MONTH(CASE WHEN (DATEPART(YEAR, DUE_DATE) = DATEPART(YEAR, PURCHASE_DATE) AND DATEPART(MONTH, DUE_DATE) >= DATEPART(MONTH, PURCHASE_DATE)) THEN DATEPART(MONTH, DUE_DATE)
WHEN DATEPART(YEAR, DUE_DATE) > DATEPART(YEAR, PURCHASE_DATE) THEN DATEPART(MONTH, DUE_DATE)
ELSE
DATEPART(MONTH, PURCHASE_DATE) END) AS VARCHAR(2)),2)
FROM TEST

on a sample table of six records generates

2012    1   2012    1   190501
2012    1   2013    12  190501
2011    1   2012    1   190501
2011    1   2011    1   190501
2012    1   2012    1   190501
2012    1   2012    2   190501

What am I doing wrong?

Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:02:21+00:00Added an answer on June 7, 2026 at 5:02 am

    Why are you trying to do it all in one go and repeating yourself all over the place.

    If you start with

    select 
    DatePart(YEAR,PURCHASE_DATE) as PURCHASE_YEAR,
    DatePart(YEAR,DUE_DATE) as DUE_YEAR,
    DatePart(MONTH,PURCHASE_DATE) as PURCHASE_MONTH,
    DatePart(MONTH,DUE_DATE) as DUE_MONTH
    From Test
    

    Then you can do

    SELECT PURCHASE_Year AS TRANSACTION_YEAR, 
           PURCHASE_Month) AS TRANSACTION_MONTH,
           CASE 
           WHEN DUE_YEAR >= PURCHASE_YEAR THEN DUE_YEAR
           ELSE PURCHASE_YEAR
           END AS RELEASE_YEAR, 
           CASE 
           WHEN (DUE_YEAR = PURCHASE_YEAR) AND (DUE_MONTH >= PURCHASE_MONTH) THEN DUE_MONTH
           WHEN DUE_YEAR > PURCHASE_YEAR THEN DUE_MONTH
           ELSE PURCHASE_MONTH 
           END AS RELEASE_MONTH,
           CAST(
            CAST(RELEASE_YEAR As VarChar(4)) + 
            '-' + 
            CAST(RELEASE_MONTH as VarChar(2)) + 
            '-1' as DATE) as RELEASE_DATE
    FROM (
    select 
    DatePart(YEAR,PURCHASE_DATE) as PURCHASE_YEAR,
    DatePart(YEAR,DUE_DATE) as DUE_YEAR,
    DatePart(MONTH,PURCHASE_DATE) as PURCHASE_MONTH,
    DatePart(MONTH,DUE_DATE) as DUE_MONTH
    From Test
    ) SomeDummyTableName
    

    I think this is what you were after, and I may even have got it right after wading through that morass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a query like this: SELECT DATEPART(year,some_date), DATEPART(month,some_date), MAX(some_value) max_value FROM some_table GROUP
SELECT YEAR(aum.AUM_Timeperiod) as Year, DATEPART(q, aum.AUM_TimePeriod) AS Quarter, SUM(cast(aum.AUM_AssetValue AS money)) as total_AssetValue FROM
I have the following query select datepart(yy, orderDate) as 'year', datename(mm, OrderDate) as 'month',
SELECT Id,Date,Name FROM people WHERE DATEPART(hh,Date) >= 7 AND DATEPART(hh,Date) <= 8 Order by
I'm currently using a select statement with one column as DATEPART(hh, CallTime) AS Hour
SELECT Dateadd(month, Datediff(month, 0, Getdate()), 0), 'First Day of Current Month' Can anyone tell
There is a datepart iso_week returning the iso_week for a selected day. SELECT DATEPART(iso_week,
How do i get the year(2009) and month(12) from the string datetime, following give
In SQL Server 2008 the isoweek can be found with this: SELECT datepart(iso_week, getdate())
I have two queries with subqueries for dateparts which I like to join. SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.