locked
DATE FORMAT MMYYYY for current date RRS feed

  • Question

  • Hi please help I need to get the current month and year Only from todays date (getdate)

    DATE FORMAT In  MMYYYY for current date

    Thanks In Advance

    Sunday, December 16, 2012 1:51 PM

Answers

All replies

  • Here is one way:

    SELECT LEFT(CONVERT(varchar,getdate(),101),2)+RIGHT(CONVERT(varchar,getdate(),101),4);
    -- 122012

    Datetime article:

    http://www.sqlusa.com/bestpractices/datetimeconversion/


    Kalman Toth
    New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2016


    • Edited by Kalman Toth Thursday, November 9, 2017 2:48 PM
    Sunday, December 16, 2012 2:06 PM
  • select cast(month(getdate()) as char(2))+cast(year(getdate())as char(4))

    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Blog: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance

    • Marked as answer by KODI_KODI Sunday, December 16, 2012 4:06 PM
    Sunday, December 16, 2012 2:10 PM
    Answerer
  • Try

    substring(convert(varchar,getdate(),112),5,2)+

    substring(convert(varchar,getdate(),112),1,4)


    Many Thanks & Best Regards, Hua Min

    Sunday, December 16, 2012 2:28 PM
  • Well, there are lot of ways to do this,

    declare @dateval datetime
    set @dateval='01 Jan 2013'
    select right ('00'+ltrim(str(datepart(month,@dateval))),2 )+convert(varchar,datepart(year,@dateval))
    --Output 
    --012013
    Regards
    Satheesh

    Sunday, December 16, 2012 3:34 PM
  • In SQL 2012:

       select format(getdate(), 'MMyyyy')


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    • Proposed as answer by Juniormint10 Wednesday, March 6, 2019 7:28 PM
    Sunday, December 16, 2012 5:00 PM
  • Thanks! 

    You could also use 'Myyyy' or 'M/yyyy' or 'M-yyyy' or 'MM-yyyy' or 'MM/yyyy'.

    By far, the simplest solution.

    juniormint

    Wednesday, March 6, 2019 7:30 PM
  • I know this is an old post but don't forget that FORMAT has been tested to be an average of 43 times slower than just about anything you can do with CONVERT or other date/time functions.

    --Jeff Moden

    Saturday, March 28, 2020 6:56 AM
  • By far, it's also the slowest solution.  FORMAT is an average of 43 times slower than CONVERT or most other temporal methods.


    --Jeff Moden

    Saturday, March 28, 2020 6:57 AM
  • Here's my take on this old post...

    SELECT STUFF(RIGHT(CONVERT(CHAR(10),GETDATE(),103),7),3,1,'');


    --Jeff Moden

    Saturday, March 28, 2020 6:58 AM