locked
DATE FORMAT MMYYYY for current date RRS feed

  • Pergunta

  • 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

    domingo, 16 de dezembro de 2012 13:51

Respostas

Todas as Respostas

  • 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


    • Editado Kalman Toth quinta-feira, 9 de novembro de 2017 14:48
    domingo, 16 de dezembro de 2012 14:06
  • 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

    • Marcado como Resposta KODI_KODI domingo, 16 de dezembro de 2012 16:06
    domingo, 16 de dezembro de 2012 14:10
    Usuário que responde
  • Try

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

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


    Many Thanks & Best Regards, Hua Min

    domingo, 16 de dezembro de 2012 14:28
  • 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

    domingo, 16 de dezembro de 2012 15:34
  • In SQL 2012:

       select format(getdate(), 'MMyyyy')


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    • Sugerido como Resposta Juniormint10 quarta-feira, 6 de março de 2019 19:28
    domingo, 16 de dezembro de 2012 17:00
  • Thanks! 

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

    By far, the simplest solution.

    juniormint

    quarta-feira, 6 de março de 2019 19:30
  • 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

    sábado, 28 de março de 2020 06:56
  • 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

    sábado, 28 de março de 2020 06:57
  • Here's my take on this old post...

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


    --Jeff Moden

    sábado, 28 de março de 2020 06:58