/*
@formatString(varchar)
- the format string to use (Examples "dd mm yyyy", "mmm.dd.yy")
Description:
Formats a given date based on the format specified in @formatString
d - one digit day (when applicable)
dd- two digit day
ddd- short day name
dddd- long day name
m- one digit month (when applicable)
mm- two digit month
mmm- short month name
mmmm- long month name
yy- two digit year
yyyy- four digit year
*/
create function dbo.fnFormatDate
(
@inputDate datetime,
@formatString varchar(25)
)
returns varchar(20) as
begin
declare @returnValue varchar(25)
-- Declare local vairables
declare @formattedDate varchar(25),
@day varchar(20), @month varchar(20), @year varchar(20),
@dayFormat varchar(5), @monthFormat varchar(5), @yearFormat varchar(5)
set @dayFormat = ''
set @monthFormat = ''
set @yearFormat = ''
-- Convert the supplied date to day mon year (25 Jan 2008)
set @formattedDate = convert(varchar, @inputDate, 106)
-- If the format string contains a format for the day
if charindex('d', @formatString) > 0
-- Get the day format string
set @dayFormat = master.dbo.fn_pcre_replace(@formatString, '.*?(d{1,4}).*', '$1')
-- If the format string contains a format for the month
if charindex('m', @formatString) > 0
-- Get the month format string
set @monthFormat = master.dbo.fn_pcre_replace(@formatString, '.*? (m{1,4}|M{1,4}).*', '$1')
-- If the format string contains a format for the year
if charindex('y', @formatString) > 0
-- Get the year format string
set @yearFormat = master.dbo.fn_pcre_replace(@formatString, '.?(y{2,4}).', '$1')
-- Format the day value based on the format string for the day
select@day =
case @dayFormat
when 'dd' then master.dbo.fn_pcre_replace(@formattedDate, '^(\d+).*', '$1')
when 'ddd' then substring(datename(dw, @formattedDate), 1, 3)
when 'dddd' then datename(dw, @formattedDate)
else convert(varchar, day(@formattedDate))
end
-- Format the month value based on the format string for the month
select@month =
case @monthFormat
when 'mm' then master.dbo.fn_pcre_replace(convert(varchar, @inputDate, 101), '^(\d+)/.*', '$1')
when 'mmm' then master.dbo.fn_pcre_replace(@formattedDate, '\d+\s(\w+)\s\d+', '$1')
when 'mmmm' then datename(m, @formattedDate)
else convert(varchar, month(@formattedDate))
end
-- Format the year value based on the format string for the year
select@year =
case @yearFormat
when 'yy' then substring(convert(varchar, year(@formattedDate)), 3, 2)
else convert(varchar, year(@formattedDate))
end
set @returnValue = @formatString
-- If the day format was specified
if @dayFormat <> ''
-- Replace the day format string with the actual day value
set @returnValue = master.dbo.fn_pcre_replace(@returnValue, @dayFormat, @day)
-- If the month format was specified
if @monthFormat <> ''
-- Replace the month format string with the actual month
set @returnValue = master.dbo.fn_pcre_replace(@returnValue, @monthFormat, @month)
-- If the year format was specified
if @yearFormat <> ''
-- Replace the year format string with the actual year
set @returnValue = master.dbo.fn_pcre_replace(@returnValue, @yearFormat, @year)
-- Return the formated value
return @returnValue
end