FormatDate - Mimics the VB Format routine for date
I don't remember where I found this little gem, but as a VB Developer it has simplified retrieving dates on many projects. Simply pass the function a datetime value and the format that you want in VB style syntax and the date is properly returned. I have modified it slightly from the original author (who I have no idea who it is) to return an empty string in the event a null date is passed.
CREATE FUNCTION dbo.fn_FormatDate (@date datetime, @format varchar(50))
RETURNS VARCHAR(50) AS
BEGIN
-------------------------------------------------------------------------------------
--
-- Name: FormatDate
--
-- Purpose: Mimics the VB Format routine for dates
--
-- Parameters:
--@date - Date, Date to be formatted
--@format - String, Template to format the date to
--
-- Returns: String, Date formatted to user requested template
--
-- Notes:
--1. Time information is not accounted for in this routine
--
--2. @format accepts the following values for each section of the date.
--Day
--dddd - Full Day Name
--ddd - Abbreviated Day Name
--dd - Zero Padded Day Number
--d - Day Number
--
--Month
--mmmm- Full Month Name
--mmm- Abbreviated Month Name
--mm- Zero Padded Month Number
--m- Month Number
--
--Year
--yyyy- Full 4 digit year
--yy- 2 digit year
--
--3. Any unexpected characters will be returned in the string
--
--4. Assumes database was set up with case-insensitive collation
--
-- Example Usage:
--All examples use the following date 2003-07-13 00:00:00
--
--'dddd, mmmm dd, yyyy' --> Sunday, July 13, 2003
--'mmddyyyy' --> 07132003
--'m-d-yy'--> 7-13-03
--'mm/dd/yyyy'--> 07/13/2003
--
-------------------------------------------------------------------------------------
DECLARE @pos AS INTEGER
DECLARE @char AS VARCHAR(1)
--
-- Replace Year
--
SET @pos = CHARINDEX('yyyy', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 4, DATENAME(yyyy, @date))
--PRINT @format
SET @pos = CHARINDEX('yyyy', @format)
END
SET @pos = CHARINDEX('yy', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 2, RIGHT(DATENAME(yyyy, @date) ,2))
--PRINT @format
SET @pos = CHARINDEX('yy', @format)
END
--
-- Replace Month
--
SET @pos = CHARINDEX('mmmm', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 4, DATENAME(month, @date))
--PRINT @format
SET @pos = CHARINDEX('mmmm', @format)
END
SET @pos = CHARINDEX('mmm', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 3, LEFT(DATENAME(month, @date), 3))
--PRINT @format
SET @pos = CHARINDEX('mmm', @format)
END
SET @pos = CHARINDEX('mm', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 2, RIGHT(('0' + CAST(DATEPART(month, @date) AS VARCHAR(2))), 2))
--PRINT @format
SET @pos = CHARINDEX('mm', @format)
END
SET @pos = CHARINDEX('m', @format)
WHILE @pos > 0
BEGIN
-- account for MArch and deceMBer
SET @char = SUBSTRING(@format, @pos + 1, 1)
IF (@char <> 'a') AND (@char <> 'b')
BEGIN
SET @format = STUFF(@format, @pos, 1, CAST(DATEPART(month, @date) AS VARCHAR(2)))
--PRINT @format
SET @pos = CHARINDEX('m', @format)
END
ELSE
BEGIN
SET @pos = CHARINDEX('m', @format, @pos + 1)
END
END
--
-- Replace Day
--
SET @pos = CHARINDEX('dddd', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 4, DATENAME(weekday, @date))
--PRINT @format
SET @pos = CHARINDEX('dddd', @format)
END
SET @pos = CHARINDEX('ddd', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 3, LEFT(DATENAME(weekday, @date), 3))
--PRINT @format
SET @pos = CHARINDEX('ddd', @format)
END
SET @pos = CHARINDEX('dd', @format)
WHILE @pos > 0
BEGIN
SET @format = STUFF(@format, @pos, 2, RIGHT(('0' + DATENAME(day, @date)), 2))
--PRINT @format
SET @pos = CHARINDEX('dd', @format)
END
SET @pos = CHARINDEX('d', @format)
WHILE @pos > 0
BEGIN
-- account for DEcember, sunDAy --> saturDAy, weDNesday
SET @char = SUBSTRING(@format, @pos + 1, 1)
IF (@char <> 'e') AND (@char <> 'a') AND (@char <> 'n')
BEGIN
SET @format = STUFF(@format, @pos, 1, CAST(DATEPART(day, @date) AS VARCHAR(2)))
--PRINT @format
SET @pos = CHARINDEX('d', @format)
END
ELSE
BEGIN
SET @pos = CHARINDEX('d', @format, @pos + 1)
END
END
IF @format = '//' BEGIN
SET @format = ''
END
RETURN @format
END