SQL date syntax

Posted on 2012-08-10
Last Modified: 2012-08-10
How do I create this 08,2012?.. The month has to be 2 digits
Question by:cheryl9063
    LVL 65

    Expert Comment

    by:Jim Horn
    -- Here's two ways to do the the month with a leading zero
    Declare @dt datetime = GETDATE()

    SELECT 'Month with a leading zero', CONVERT(VARCHAR(2), @dt,101), RIGHT('0' + CAST(Month(@dt) as varchar(2)), 2)

    Are you trying to return MM{comma}YYYY?
    LVL 65

    Accepted Solution

    >Are you trying to return MM{comma}YYYY?
    If yes, then (replace @dt in the second line with whatever date you're trying to mainipulate)...

    Declare @dt datetime = GETDATE()

    SELECT CONVERT(VARCHAR(2), @dt,101) + ',' + CAST(YEAR(@dt) as char(4))
    LVL 65

    Expert Comment

    by:Jim Horn
    Thanks for the grade.  Good luck with your project.  -Jim

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
    There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    732 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

    25 Experts available now in Live!

    Get 1:1 Help Now