Skip to content

Instantly share code, notes, and snippets.

@doug-wade
Last active December 12, 2015 09:19
Show Gist options
  • Save doug-wade/4750589 to your computer and use it in GitHub Desktop.
Save doug-wade/4750589 to your computer and use it in GitHub Desktop.
creates a date from integers representing day/month/year
if exists (
select *
from sys.objects
where object_id = OBJECT_ID(N'[dbo].[dateSerial]')
and type in (N'FN', N'IF', N'TF', N'FS', N'FT')
)
begin
drop function [dbo].[dateSerial]
end
go
set ansi_nulls on
go
set quoted_identifier on
go
set ansi_padding on
go
create function dbo.fnDateSerial (
@day int
,@month int
,@year int
)
returns datetime
as
/*here's what's up
Written by: Doug Wade
On: 2013-03-04
Does: creates a date from a day/month/year
*/
begin
declare @date datetime
set @date = dateAdd(Year, @year - 1900, 0)
set @date = dateAdd(Month, @month - 1, @date)
set @date = dateAdd(Day, @day - 1, @date)
if datepart(month,@date) <> @month or datepart(day,@date) <> @day OR datepart(year,@date) <> @year
begin
set @date = null
end
return @date
end
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment