Convert getdate() to CYYMMDD

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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
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?

NBAISAuthor Commented:
CCYYMMDD = YYYYMMDD, but CYYMMDD is apparently different.
Kevin CrossChief Technology OfficerCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

I think you can get your answers here...This article explains step by step procedure to covert SQL date into AS400 date
Kevin CrossChief Technology OfficerCommented:
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.
Mark WillsTopic AdvisorCommented:
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)
NBAISAuthor Commented:
Thank you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.