Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need Query for getting MONTHNAME,YEAR from my DateColumn SQLSERVER

Need Query for getting MONTHNAME,YEAR from my DateColumn SQLSERVER

Scheduled Pinned Locked Moved Database
database
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kish20
    wrote on last edited by
    #1

    Hi, I have a column named posted date, Depending on the above column I need to count No of records per year and month wise asc. I done by using group by function but am not able to get jan --- desc order. Result ie:MonthName,Year,Count columns

    B I 2 Replies Last reply
    0
    • K kish20

      Hi, I have a column named posted date, Depending on the above column I need to count No of records per year and month wise asc. I done by using group by function but am not able to get jan --- desc order. Result ie:MonthName,Year,Count columns

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Show your code what you did so far.


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      1 Reply Last reply
      0
      • K kish20

        Hi, I have a column named posted date, Depending on the above column I need to count No of records per year and month wise asc. I done by using group by function but am not able to get jan --- desc order. Result ie:MonthName,Year,Count columns

        I Offline
        I Offline
        infneeta
        wrote on last edited by
        #3

        Hi, I have below records in my table 'Sales' ID SalesName LastUpdated ------------------------------------------------- 1 S1 2009-07-16 13:27:23.890 2 S2 2009-08-16 13:32:58.127 3 S3 2009-08-16 13:33:01.987 4 S4 2009-08-16 13:34:21.733 5 S5 2009-09-16 13:32:40.703 6 S6 2011-01-16 13:32:23.703 7 S7 2011-02-16 13:32:23.703 8 S8 2011-02-16 13:32:23.703 9 S9 2011-03-16 13:32:23.703 10 S10 2011-07-16 13:32:23.703 -------------------------------------------------- Below query gives me the count of sales for different months on yearly basis. SELECT DISTINCT MonthCount = (SELECT COUNT(DATEPART("mm",LastUpdated)) FROM Sales WHERE DATEPART("yy",LastUpdated) = DATEPART("yy",p.LastUpdated) AND DATEPART("mm",LastUpdated) = DATEPART("mm",p.LastUpdated)) , "Month" = DATENAME(Month,LastUpdated),"Year"=DATEPART("yy",p.LastUpdated) FROM Sales p ORDER BY DATEPART("yy",p.LastUpdated) Here is my desired result: MonthCount Month Year ----------------------------- 1 July 2009 1 September 2009 3 August 2009 1 January 2011 1 July 2011 1 March 2011 2 February 2011

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups