Display Dates with leading zero

  • Hi all

    I have date data that is stored as integers in 3 sperate columns e.g.

    IntYear (2009), IntMonth(5), IntDay(3). I want to concatenate the 3 values to display 20090503. I am not sure as to the best way to add the leading zero when I convert the and the 3 columns. I have tried using convert with the various date formats. However I end up with 200953.

    Thanks

  • You could try something like this:

    DECLARE

    @Year INT,

    @Month INT,

    @Day INT

    SET @Day = 3

    SET @Month = 5

    SET @Year = 2009

    SELECT

    CAST(@Year AS CHAR(4)) +

    RIGHT('00' + CAST(@Month AS VARCHAR(2)),2) +

    RIGHT('00' + CAST(@day AS VARCHAR(2)),2)

  • Thanks for the help that did the trick. I eventually had to Convert it back to an integer but that got it in the right format.:-)

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

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