Getdate and Datepart

  • Hello All,

    I'm looking for options on using getdate. What I'm trying to get from getdate () is

    01-2006

    On the fly my first thought was:

    set @v_reportdate = getdate() -21

    set @v_GetMoYear = CAST (DATEPART(MM, @v_reportdate)AS VARCHAR) + '-' + CAST (DATEPART(YY, @v_reportdate)AS VARCHAR)

    This works and returns 1-2006. Any other options are welcome.

    Thanks,

    Rick

  • declare @year char(4),@month char(2)

    set @year=convert(char(4),getdate(),120)

    set @month=right(convert(char(7),getdate(),120),2)

    select @month + '-' + @year

     

    -Krishnan

  • Thanks Krishnan!

    That is much cleaner.

    Rick-

  • or

    SELECT STUFF(CONVERT(varchar,GETDATE(),110),3,3,'')

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply