Convert getdate() to CYYMMDD

Posted on 2008-11-11
Last Modified: 2012-05-05
In SQL 2005 how do I convert getdate() to CYYMMDD.  The only thing I can find is something about AS400 and take convert one of these broken dates back to normal ones.  Any ideas?
Question by:NBAIS
    LVL 59

    Expert Comment

    by:Kevin Cross
    I believe you want CONVERT() function with code 112 (ISO):

    If my understanding is correct, CYYMMDD is same as YYYYMMDD as C is century, right?


    Author Comment

    CCYYMMDD = YYYYMMDD, but CYYMMDD is apparently different.
    LVL 59

    Accepted Solution

    Try this - think iSeries dates come out like 01081111, so need to do more like this:
    +Convert to YYYYMMDD format.
    +Minus 19000000 from the resultant number (i.e. 20081111).
    +Convert back to string and concatenate with '00000000' for padding to ensure that 1081111 becomes 01081111 when we take right most 8 characters.
    SELECT RIGHT('00000000' + LTRIM(STR(CONVERT(VARCHAR(8), GETDATE(), 112) - 19000000)), 8)

    Open in new window

    LVL 2

    Expert Comment

    I think you can get your answers here...This article explains step by step procedure to covert SQL date into AS400 date
    LVL 59

    Expert Comment

    by:Kevin Cross
    That is exactly what I had except they are only taking 7 characters, so you end up with 1081111.  Just change my code snippet above from:

    SELECT RIGHT('00000000' + LTRIM(STR(CONVERT(VARCHAR(8), GETDATE(), 112) - 19000000)), 8)


    SELECT RIGHT('0000000' + LTRIM(STR(CONVERT(VARCHAR(8), GETDATE(), 112) - 19000000)), 7)

    It is a flaw to only add one '0' as they have it though if you consider this date which occurs when you use 0 in SQL:

    19000101-19000000 = 101
    '0' + 101 = '0101'

    This is not too big a deal since you are using getdate here, but programmitically why not be prepared as typing the 7 '0' is not that much harder. :)  Just my humble opinion though.
    LVL 51

    Expert Comment

    by:Mark Wills
    can also do  using the same format numbers, except subtract 100 to get two digit years (ie format 112 becomes 12):

    using @d to emulate our datetime column or as a variable...

    declare @d datetime
    set @d = '19881126'

    SELECT convert(varchar,left(year(@d),2) - 19) + convert(VARCHAR(6), @d, 12)


    SELECT convert(varchar,left(year(getdate()),2) - 19) + convert(VARCHAR(6), getdate(), 12)

    Author Closing Comment

    Thank you for your help.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now