create function get_format_date(@date datetime, @type int)
returns varchar(36)
begin
declare @result varchar(36)
declare @time char(8)
declare @y varchar(4)
declare @m varchar(2)
declare @d varchar(2)
declare @p varchar(4)
declare @h varchar(2)
declare @n varchar(2)
declare @s varchar(2)
if(isdate(@date) = 1)
begin
set @time = convert(varchar(8), @date, 8)
set @y = cast(year(@date) as varchar(4))
set @m = cast(month(@date) as varchar(2))
set @d = cast(day(@date) as varchar(2))
set @h = substring(@time, 1, charindex(':', @time, 1)-1)
set @time = substring(@time, charindex(':', @time, 1)+1, 8)
set @n = substring(@time, 1, charindex(':', @time, 1)-1)
set @time = substring(@time, charindex(':', @time, 1)+1, 8)
set @s = @time
set @result = @y + '년' + @m + '월' + @d + '일 ' + @h + '시' + @n + '분' + @s + '초'
end
else
begin
set @result = '날짜 형식이 맞지 않음'
end
return @result
end
--실행방법
select boardsample.dbo.get_format_date(getdate(), 1)
댓글 영역