Month(GetDate()) to return '04' instead of 4

Posted on 2006-04-20
Last Modified: 2008-03-06
Hi all

I have the following SP line in progress...

declare @time_period_id_current int
set @time_period_id_current = CAST(CAST(YEAR(GETDATE()) as char(4))+ '' + CAST(MONTH(GETDATE()) as char(4))as int)

Where I'm trying to convert the current year and month into and integer with YYYYMM format, i.e. 200604.  The above line returns 20064.

How to change the above line to retain the leading '0' in the month?

Thanks in advance.
Question by:Jim Horn
    LVL 75

    Accepted Solution

    select RIGHT('00'+CAST(MONTH(GETDATE()) as varchar(2)),2)
    LVL 38

    Expert Comment

    by:Jim P.
    Right('00' + CAST(MONTH(GETDATE()) as char(4))as int) ,2)
    LVL 38

    Assisted Solution

    by:Jim P.
    anees is correct. But yyou can get away without the sizing requirement.

    select RIGHT('00'+CAST(MONTH(GETDATE()) as varchar),2)
    LVL 65

    Author Comment

    by:Jim Horn
    Thanks guys.  -Jim
    LVL 38

    Expert Comment

    by:Jim P.
    Glad to be of assistance. May all your days get brighter and brighter.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Viewers will learn how the fundamental information of how to create a table.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now