Have you looked at the new FORMAT function in SQL Server 2012?

http://msdn.microsoft.com/en-us/library/hh213505.aspx

You should be able to use something like this:

SELECT FORMAT(YourDateColumn, 'yyyyMMdd')

or whatever you really want to use - basically, the same formatting options as in C# / .NET are available when using FORMAT

Answer from marc_s on Stack Overflow
🌐
Experts Exchange
experts-exchange.com › questions › 25048724 › convert-Date-to-CCYYMMDD-format-in-SQL.html
Solved: convert Date() to CCYYMMDD format in SQL | Experts Exchange
January 12, 2010 - create Function dbo.FormatCCDate(@inputdate Datetime) returns varchar as begin --reformats a date in mm/dd/yyyy format to cyymmdd declare @lngyear bigint declare @lngday bigint declare @lngmonth bigint set @lngyear = year(@inputdate) set @lngyear ...
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › date-conversion-to-ccyymmdd
Date conversion to CCYYMMDD – SQLServerCentral Forums
September 22, 2007 - I'm new to T-SQL and waiting for some reference materials to arrive. Any good reference book suggestions would be greatly appreciated. ... Assuming that the column holding your dates is a datetime datatype, you can convert it to a string and use a style parameter.
Discussions

Convert Date to CYYMMDD SQL-Server - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
How to write the code in Oracle SQL (like 'CCYYMMDD' into 102 ) - Stack Overflow
How to write the code in Oracle SQL (like 'CCYYMMDD' into 102 )? If someone will enter the date in the frontend, the value should return as 102 instead of date. For that, how will write the functio... More on stackoverflow.com
🌐 stackoverflow.com
Convert getdate() to CYYMMDD
CCYYMMDD = YYYYMMDD, but CYYMMDD is apparently different. ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... 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... More on experts-exchange.com
🌐 experts-exchange.com
October 1, 2008
substring - Change sql Get date to format ccmmdd? - Stack Overflow
I am trying to find out how to change my get date into the format of ccmmdd? So far the closest iv come is ccyymmdd with: CONVERT(VARCHAR(8),GETDATE(),112) This produces 20140505, What i'm lookin... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft
social.msdn.microsoft.com › Forums › sqlserver › en-US › 5b98b6db-48b1-4861-9d21-ee3564dd194a › formatting-a-date-as-ccyymmdd
Formatting a date as CCYY-MM-DD | Microsoft Learn
October 13, 2008 - SET ProcessDate = CAST(YEAR(GETDATE()) AS CHAR(4))+'-'+RIGHT('0'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2)+'-'+RIGHT('0'+CAST(DAY(GETDATE()) AS VARCHAR(2)),2) ... Try using convert instead of cast, as you can format the date string.
Top answer
1 of 3
2

You cannot write a function to determine which numeric date string corresponds to which format as the date string could be multiple formats:

For example, 010203 could be:

  • Format 2: DDMMYY 1st February 03
  • Format 3: MMDDYY 2nd January 03
  • Format 101: YYMMDD 3rd February 01
  • Format 306: DDHHMM 02:03 of Day 1
  • Format 402: HHMMSS 01:02:03
  • Format 405: MMMMSS 102 minutes 3 seconds
  • Format 610: CCYYMM March 0102
  • Format 616: CCYYWW Week 3 of 0102

Similarly 10080102 could be:

  • Format 4: DDMMCCYY 10th August 102
  • Format 102: CCYYMMDD 2nd January 1008
  • Format 305: MMDDHHMM 8th October 01:02
  • Format 501: HHMMHHMM Time span from 10:08 to 01:01

If anyone inserts the date in this format "CCYYMMDD" then the value should return only 102 as a default in the frontend. How to write the code in SQL?

You cannot, as I described above it is ambiguous what some values are and they could return multiple formats. Instead you should create another column and store the date format in that when the user inputs the date rather than trying to reconstruct the format code from an (ambiguous) number.

2 of 3
0

You can verify that a string fits a date format like this:

create or replace function is_CCYYMMDD 
    (p_str in varchar2)
    return boolean
is
    rv boolean;
    dt date;
begin
    begin
        dt := to_date(p_str, 'YYYYMMDD');
        dt := to_date('2000' || substr(p_str, 5), 'YYYYMMDD');
        rv := true; 
    exception
        when others then
            rv := false;
    end;
    return rv;
end;

The first to_date() just checks that the whole string is a valid date. The second to_date() checks that the second half of the string is a valid month and day combo. This partly addresses @MTO observations about the problems of enforcing a strict format when some strings can fit more than one format.

Note that it is perfectly possible to have valid dates which pass this test despite being ambiguous e.g. is 20111012 in 'YYYYMMDD' or 'DDMMYYYY'? There is no way to be sure unless you enforce strict date formatting in the front-end input by using a date picker widget or separate input boxes for year, month and day.

why you pass '2000' in this query?

The second check verifies that the last four characters are valid as month and day. Whenever we do this sort of test we run into the problem of leap years. If we just apply to_date(str, 'MMDD') Oracle will default the year to the current year: the snag is 2018029 is not a valid date even though the original input of 20160229 is valid. My function avoids this by fixing the year element to 2000, which was a leap year.


Incidentally, if you want to use this function in SQL you'll need to change the return datatype to varchar2 (Y/N flag) or a number (1/0) because SQL doesn't support Booolean.

🌐
TechTalkz
techtalkz.com › home › forums › tech support archives › microsoft › microsoft sql server
MSSQL default field value 'GETDATE() as CCYYMMDD | TechTalkz.com - Technology and Computer Help Forums
October 28, 2007 - CONVERT with a 112 style will convert a datetime value to this ISO string representation: SELECT CONVERT(char(8), GETDATE(), 112) However, you might consider storing dates in native format and performing string formatting in application code during retrieval.
🌐
Experts Exchange
experts-exchange.com › questions › 23896625 › Convert-getdate-to-CYYMMDD.html
Solved: Convert getdate() to CYYMMDD | Experts Exchange
October 1, 2008 - 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( ... Thank you for your help.
Find elsewhere
🌐
MSSQLTips
mssqltips.com › home › sql convert date to yyyymmdd
SQL Convert Date to YYYYMMDD
May 27, 2025 - Results: Example SQL Server dates loaded. Next, converting a DATE and DATETIME datatype to character 8 ‘yyyymmdd’ output using CONVERT and FORMAT functions.
🌐
CopyProgramming
copyprogramming.com › howto › convert-date-format-to-ccyymmdd-hh-mm-ss
Transforming date format to CCYYMMDD HH:MM:SS - Sql server
June 18, 2023 - How to convert "yyyyMMdd" date ... that accept cc or CC for century.. Edit: no conversion necessary. Since ccyy means yyyy, there is no conversion to do from yyyyMMdd to ccyyMMdd.The string you already got in …...
🌐
Stack Overflow
stackoverflow.com › questions › 59530793 › storing-date-in-ccyymmdd-format-in-teradata
sql - storing date in 'CCYYMMDD' format in Teradata - Stack Overflow
December 30, 2019 - I would like to store dates in the format CCYYMMDD in Teradata, but I fail to do so. Find below what I tried so far: query 1: SEL CAST(CAST(CURRENT_DATE AS DATE FORMAT 'YYYYMMDD') AS VARCHAR(8)) --
🌐
Database Guide
database.guide › convert-date-to-yyyymmdd-in-sql-server
Convert DATE to YYYYMMDD in SQL Server
February 24, 2023 - In SQL Server, we can use functions like CONVERT() or FORMAT() to convert a valid date type into a format like yyyymmdd.
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › dbms packages › microsoft sql server: programming
Convert cyymmdd to mmddyyyy - Microsoft SQL Server: Programming | Tek-Tips
December 4, 2006 - ... or more specifically the 'correct' way to convert the date coming from the iSeries to the mm/dd/yyyy format? In other words, is there a way to add, convert, and arrange the date info in one step? ... Add 19000000, then convert to string, then convert to date.
🌐
MSSQLTips
mssqltips.com › home › sql date format examples using convert function
SQL Date Format Examples using SQL CONVERT Function
September 26, 2025 - SQL Server provides a number of date and time formatting options and in this article we look at how to use SQL CONVERT to output different date/time formats such as mm/dd/yy, mm/dd/yyyy, dd-mm-yyyy, etc.
🌐
CodingSight
codingsight.com › home › converting datetime to yyyy-mm-dd format in sql server
How to Convert DateTime to Date Format in SQL Server
July 24, 2023 - To convert all strings in the Arriv_Date column to the SQL Server date format YYYY-MM-DD, you can again use the CAST function. You need to specify the column name followed by the AS statement and the DATE type.