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 246567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:11:00+00:00 2026-05-11T21:11:00+00:00

I need distinct year and month from date column which would be sorted by

  • 0

I need distinct year and month from date column which would be sorted by same column.

I have date coulmn with values like (YYYY/MM/DD)

2007/11/7
2007/1/8
2007/11/4
2007/12/3
2008/10/4
2009/11/5
2008/5/16

after having query, it should be

2007/1/1
2007/11/1
2007/12/1
2008/5/1
2008/10/1
2009/11/1

This doesn’t seems to be working

SELECT distinct (cast(year(datecol) as nvarchar(20) ) +
       '/'+ cast(month(datecol) as nvarchar(20) ) + '/1') as dt1 
FROM Table 
ORDER BY dt1
  • 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-05-11T21:11:00+00:00Added an answer on May 11, 2026 at 9:11 pm

    Soemthing like this would work on MS SQL Server:

    select 
        distinct 
        dateadd(day, -1 * DAY(datefield) + 1, datefield)
    From
        datetable
    order by
        dateadd(day, -1 * DAY(datefield) + 1, datefield) 
    

    The DATEADD function call basically subtracts (day-1) DAYS from the current date –> you always get the first of whatever month that date is in.

    Sort by it and you’re done! 🙂

    ADditionally, you could also add this functionality to your table as a “computed column” and then use that for easy acccess:

    alter table yourTable 
      add FirstOfMonth As DATEADD(day, -1 * DAY(datefield) + 1, datefield) persisted
    

    Then your query would be even simpler:

    SELECT DISTINCT FirstOfMonth
    FROM YourTable
    ORDER BY FirstOfMonth
    

    Marc

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I really doubt it makes any difference at all. May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer I'd say two tables Action = {id, type_id, details, user_id}… May 12, 2026 at 12:10 am
  • Editorial Team
    Editorial Team added an answer You have a couple of options, all of which suck.… May 12, 2026 at 12:10 am

Related Questions

I need to know if there's a best way to optimize this kind of
This was originally a question I wanted to ask, but while researching the details
I have a List<> of objects in C# and I need a way to
I have the following item set from an XML: id category 5 1 5

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.