[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert getdate() to CYYMMDD

Posted on 2008-11-11
7
Medium Priority
?
4,052 Views
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?
0
Comment
Question by:NBAIS
7 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22936212
I believe you want CONVERT() function with code 112 (ISO):
http://msdn.microsoft.com/en-us/library/ms187928.aspx

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

SELECT CONVERT(VARCHAR(8), GETDATE(), 112)
0
 

Author Comment

by:NBAIS
ID: 22936230
CCYYMMDD = YYYYMMDD, but CYYMMDD is apparently different.
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22936246
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

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 2

Expert Comment

by:rajeshprasath
ID: 22937446
I think you can get your answers here...This article explains step by step procedure to covert SQL date into AS400 date

http://www.scriptsahead.com/sql-server/t-sql/convert-sql-date-into-as400-date-format
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22938555
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)

To:

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

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.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22974367
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)

--or

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

Author Closing Comment

by:NBAIS
ID: 31515796
Thank you for your help.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline

873 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