The ISO 8601 standard says dates should look like YYYY-MM-DD to avoid confusion between formats like MM/DD/YYYY or DD/MM/YYYY. But sometimes you might need to remove the hyphens and display the date as YYYYMMDD. Maybe your software doesn’t accept special characters, or you’re trying to save space. Whatever the case, here are some simple ways to get today’s date into YYYYMMDD format.
Method 1: Using the FORMAT() function
The FORMAT() function enables us to convert values into a nicely formatted string. And this includes date values. So if we want the current date in YYYYMMDD, we can do this:
SELECT FORMAT(GETDATE(), 'yyyyMMdd');
Example result:
20250805
The GETDATE() function returns the current date as a datetime value. We then use FORMAT() to format that date. The 'yyyyMMdd' custom format string directly translates to a four-digit year, two-digit month, and two-digit day without any separators. That’s because we explicitly specified them in that format.
The FORMAT() function was introduced in SQL Server 2012 and relies on the .NET Framework Common Language Runtime (CLR). If you’re using an older version of SQL Server, or you aren’t able to utilize the .NET framework, try the next option.
Method 2: Using the CONVERT() Function with a Style Code
The CONVERT() function allows you to transform the current date into a string with a specific style. The function accepts an optional style argument. This argument tells the function which style to use for the output.
Example:
SELECT CONVERT(char(8), GETDATE(), 112);
Output:
20250805
The style code 112 is what formats the result as YYYYMMDD. We use char(8) to ensure the output string has exactly eight characters. There are many other styles that we could use if needed.
Method 3: Using the REPLACE() Function
If you have a date that’s already in the YYYY-MM-DD format and need to remove the separators, you can achieve the YYYYMMDD format simply by removing the separators. This can be done with the REPLACE() function.
Here’s a simplistic example that uses a string literal for the date:
SELECT REPLACE('2025-08-05', '-', '');
Output:
20250805
In practice, the date may already have some conversion/casting done in order to produce it in YYYY-MM-DD format:
SELECT REPLACE(CAST(GETDATE() AS DATE), '-', '');
Output:
20250805
Here’s an example that uses the CONVERT() function:
SELECT REPLACE(CONVERT(varchar(10), GETDATE(), 23), '-', '');
Output:
20250805
The REPLACE() function takes the date string (in this case, from CONVERT() using style 23) and replaces every instance of the hyphen - with an empty string '', which effectively removes the dashes and gives you the desired YYYYMMDD output.
You would probably only consider this option if you’re on a system that already uses CONVERT() to produce YYYY-MM-DD and you can’t (or don’t want to) change it. In such cases you could use REPLACE() to do the last bit of formatting.