Skip to content

Instantly share code, notes, and snippets.

@MarkPryceMaherMSFT
Last active April 1, 2022 18:02
Show Gist options
  • Save MarkPryceMaherMSFT/d2cf8675bc74cd592db0f64e7621d0d3 to your computer and use it in GitHub Desktop.
Save MarkPryceMaherMSFT/d2cf8675bc74cd592db0f64e7621d0d3 to your computer and use it in GitHub Desktop.
create FUNCTION dbo.check_my_date (@value varchar(255))
RETURNS datetime2
as
BEGIN
declare @rt datetime2;
set @rt = convert(datetime2,'19000101') -- default value
if @value = '0' -- special case
begin
set @rt = convert(datetime2,'00010101')
end
else
begin
set @rt = try_parse(@value as datetime2)
-- logic around dates before a certain date
if try_parse(@value as datetime2) < convert(datetime2,'19000101')
begin
set @rt = convert(datetime2,'19000101')
end
end
return @rt;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment