Ask the Author: Date Formatting in SQL
The question came from a reader.
how do I format a data that looks like 2006-03-12 00:00:00 to MMDDYYYY format and have the month and days all be two characters long?
In most cases, one would just return a date field from the database and format it for display using ColdFusion (or your language of choice).
But, if for whatever reason you need to format a date in SQL, there are some options. The reader was using SQL Server, so the rest of this article will assume that we are dealing with SQL Server.
First, you should look at the convert function. The convert function is used for date formatting in SQL. Unfortunately, it does not have the flexibility that CF offers you with its' mask parameter. It accepts three parameters:
- Date Type: The data type is the type of the result after a convert. Since we're trying to format a date, most likely this will be char or varchar.
- Expression: In the case of dates, this is the date (or a SQL expression that results in date)
- Format Code: This is a code that specifies the format of the resulting date. A full list of codes, and their returned date formats, is listed in documentation.
The reader was asking about formatting the date using MMDDYYYY. Unfortunately, none of the date formats offer that specifically. 112 is the closest, which is YYYYMMDD. You can use this:
select CONVERT(varchar(8),GETDATE(),112)
( Note: For the data type, I'm going to use a varchar(8), since we want 8 characters in the result )
This will give us 20061213 . Close, but not what was asked for. 101 and 110 will give us the data in the right order, but not in the required format:
select CONVERT(varchar(10),GETDATE(),110)
select CONVERT(varchar(10),GETDATE(),101)
Will give us '12-13-2006' and '12/13/2006' respectively. I could have used the SUBSTRING function to wipe out the '-' or '/' but I didn't chose that route. Instead I chose to use the datepart function.
Datepart accepts 2 arguments:
- The Date Part: The date part specifies the piece of the date you want to get. In this example, we are worried about the year (yyyy), the month (mm), and the day (dd)
- The Date: this is the date you want to extra parts of.
So, using the datepart function we can piece together something like this:
select cast(datepart(month,getdate()) as varchar(2)) + cast(datepart(day,getdate()) as varchar(2)) + cast(datepart(year,getdate()) as varchar(4))
Which will return 12132006. I'm using cast to return all various date parts as string values. And I'm using the plus (+) sign to concatenate the results.
I've solved the problem solved, right?
Almost. It turns out that datepart will not return leading zeros for one digit days or one digit months. That would violate our requested format.
You can use a case inside SQL to solve this. A case in SQL is just like a cfif statement in ColdFusion. Here is an example that will give us the month:
select
case when datepart(month,getdate()) <= 9 then cast('0' + cast(datepart(month,getdate()) as varchar(1)) as varchar(2)) else cast(datepart(month,getdate()) as varchar(2)) end
This will return the month. It, basically, says if the current month is less than or equal to 9 (AKA A single digit) return the character '0' concatenated with month. Else return the month w/ no extra characters because it's already two digits.
Now add in the day:
select
case when datepart(month,getdate()) <= 9 then cast('0' + cast(datepart(month,getdate()) as varchar(1)) as varchar(2)) else cast(datepart(month,getdate()) as varchar(2)) end +
case when datepart(day,getdate()) <= 9 then cast('0' + cast(datepart(day,getdate()) as varchar(1)) as varchar(2)) else cast(datepart(day,getdate()) as varchar(2)) end
The day code is, basically, the same code except the datepart gets the day, not the month.
And finally, add in the year:
select
case when datepart(month,getdate()) <= 9 then cast('0' + cast(datepart(month,getdate()) as varchar(1)) as varchar(2)) else cast(datepart(month,getdate()) as varchar(2)) end +
case when datepart(day,getdate()) <= 9 then cast('0' + cast(datepart(day,getdate()) as varchar(1)) as varchar(2)) else cast(datepart(day,getdate()) as varchar(2)) end +
cast(datepart(year,getdate()) as varchar(4))
And bingo, I have a happy reader with a happy client.
Now, this is a lot of code and it will make your SQL statement look sloppy and hard to read and hard to maintain. My next step was to move this information into a SQL User Defined Function. I created a SQL User Defined Function to format the date like this:
CREATE FUNCTION FormatDate ( @mydate datetime )
RETURNS varchar(8) AS
BEGIN
return (
case when datepart(month,@mydate) <= 9 then
cast('0' + cast(datepart(month,@mydate) as varchar(1)) as varchar(2))
else
cast(datepart(month,@mydate) as varchar(2))
end +
case when datepart(day,@mydate) <= 9 then
cast('0' + cast(datepart(day,@mydate) as varchar(1)) as varchar(2))
else
cast(datepart(day,@mydate) as varchar(2))
end +
cast(datepart(year,@mydate) as varchar(4)) )
END
You can call it from code like this:
select dbo.FormatDate(getdate())




