+ Reply to Thread
Results 1 to 7 of 7

How to get a two-digit month value

  1. How to get a two-digit month value

    Howdy.

    I'm attempting to use DATEPART to give me a two-digit month, and further, to
    cast that as a nvarchar. It is rendering March as '3' instead of '03'.

    Is there a way to get '03' here????

    Thanks.

    Danny

  2. Re: How to get a two-digit month value

    Try:

    select
    replace (str (datepart (mm, getdate ()), 2), ' ', '0')


    --
    Tom

    ----------------------------------------------------
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
    SQL Server MVP
    Toronto, ON Canada
    https://mvp.support.microsoft.com/profile/Tom.Moreau


    "Danny" wrote in message
    newsBD73AE0-D3A5-4747-A703-F9AE99B28633@microsoft.com...
    Howdy.

    I'm attempting to use DATEPART to give me a two-digit month, and further, to
    cast that as a nvarchar. It is rendering March as '3' instead of '03'.

    Is there a way to get '03' here????

    Thanks.

    Danny



  3. Re: How to get a two-digit month value

    SELECT RIGHT('0' + RTRIM(MONTH(GETDATE())), 2);


    "Danny" wrote in message
    newsBD73AE0-D3A5-4747-A703-F9AE99B28633@microsoft.com...
    > Howdy.
    >
    > I'm attempting to use DATEPART to give me a two-digit month, and further,
    > to
    > cast that as a nvarchar. It is rendering March as '3' instead of '03'.
    >
    > Is there a way to get '03' here????
    >
    > Thanks.
    >
    > Danny




  4. Re: How to get a two-digit month value

    On Jun 10, 2:23 pm, Danny wrote:
    > Howdy.
    >
    > I'm attempting to use DATEPART to give me a two-digit month, and further, to
    > cast that as a nvarchar. It is rendering March as '3' instead of '03'.
    >
    > Is there a way to get '03' here????
    >
    > Thanks.
    >
    > Danny


    SELECT RIGHT(CAST(100 + MONTH(GetDate()) AS CHAR(3)),2)

  5. Re: How to get a two-digit month value

    yes, this works. Thank you Aaron.

    "Aaron Bertrand [SQL Server MVP]" wrote:

    > SELECT RIGHT('0' + RTRIM(MONTH(GETDATE())), 2);
    >
    >
    > "Danny" wrote in message
    > newsBD73AE0-D3A5-4747-A703-F9AE99B28633@microsoft.com...
    > > Howdy.
    > >
    > > I'm attempting to use DATEPART to give me a two-digit month, and further,
    > > to
    > > cast that as a nvarchar. It is rendering March as '3' instead of '03'.
    > >
    > > Is there a way to get '03' here????
    > >
    > > Thanks.
    > >
    > > Danny

    >
    >
    >


  6. Re: How to get a two-digit month value

    Yet another solution:

    SELECT SUBSTRING(CONVERT(nvarchar(6), CURRENT_TIMESTAMP, 112),5,2)

    --
    Gert-Jan
    SQL Server MVP

  7. RE: How to get a two-digit month value

    Thank you all for your help - greatly appreciated.

    "Danny" wrote:

    > Howdy.
    >
    > I'm attempting to use DATEPART to give me a two-digit month, and further, to
    > cast that as a nvarchar. It is rendering March as '3' instead of '03'.
    >
    > Is there a way to get '03' here????
    >
    > Thanks.
    >
    > Danny


+ Reply to Thread