:SetVar Procedure "AUX:TryMakeDateFromStr"
:r $(DropProcedureScript_AUX)
create procedure [$(Procedure)] @DateString              $(dt_String_AUX),
                                @ResultDate              datetime2 out,
                                @SignificantLengthStrict bit = 0
AS
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# STRING TO DATE CONVERSION ATTEMPT, IN SIMPLE LOCAL FORMAT (ACCODING TO EFFECTIVE LOCALE):
# - INPUT STRING EN: MM/DD/YYYY (DEFAULT LANGUAGE LOCALE);
# - INPUT STRING RU: ДД.ММ.ГГГГ (MAIN LANGUAGE LOCALE);
# - INPUT STRING XX: ...
#
# === Usage: ===
# exec [AUX:TryMakeDateFromStr] <Date-строка>, @<Resulting date variable> out
# exec [AUX:TryMakeDateFromStr] <Date-строка>, @<Resulting date variable> out, 1
#
# Return values
# : 0 ————————— success;
# : not zero —— error
#
==================================================================================================*/
$(BEGIN_AUX)
----------------------------------------------------------------------------------------------------

if @DateString is null
   exec [AUX:RaiseIncorrectProcedureCall] '$(Procedure)'

----------------------------------------------------------------------------------------------------
set @ResultDate = null

if @DateString = TRIM( @DateString )

if CHARINDEX(' ',@DateString) > 0
   return 1

declare @Length int
set @Length = LEN( @DateString )
if @Length not between 9 and 10 or
   @SignificantLengthStrict=1 and @Length<>10
   return 1

declare @IsInMainLanguageLocale bit, @IsInDefaultLangLocale bit
select
   @IsInMainLanguageLocale = dbo.[AUX:IsInMainLanguageLocale](), -- RU
   @IsInDefaultLangLocale = dbo.[AUX:IsInDefaultLangLocale]()
if @IsInMainLanguageLocale = 1 or @IsInDefaultLangLocale = 1
begin
   declare @P int, @Ch nchar
   set @P = 1
   while @P <= @Length
   begin
      set @Ch = SUBSTRING( @DateString, @P, 1 )
      if @Ch not between '0' and '9' and (
         @IsInMainLanguageLocale = 1 and @Ch<>'.' or
         @IsInDefaultLangLocale = 1 and @Ch<>'/' )
         return 1
      set @P = @P + 1
   end
end

declare @Date datetime2
begin try
   set @Date = CONVERT( datetime2, @DateString, dbo.[AUX:DateFormat]() )
end try
begin catch
   return 1
end catch

if dbo.[AUX:IsPureDate]( @Date ) = 0
   return 1

set @ResultDate = @Date

----------------------------------------------------------------------------------------------------
$(END_AUX)
/*================================================================================================*/
GO
/*
declare @Date datetime2
exec [AUX:TryMakeDateFromStr] '10/12/2015', @Date out; select @Date '10/12/2015'
exec [AUX:TryMakeDateFromStr] '10/02/2015', @Date out; select @Date '10/02/2015'
exec [AUX:TryMakeDateFromStr] '10/02/2015', @Date out, 1; select @Date '10/02/2015'
exec [AUX:TryMakeDateFromStr] N'AB/02/2015', @Date out; select @Date N'AB/02/2015'
*/
/*
declare @Date datetime2
exec [AUX:TryMakeDateFromStr] '12.10.2015', @Date out; select @Date '12.10.2015'
exec [AUX:TryMakeDateFromStr] '2.10.2015', @Date out; select @Date '2.10.2015'
exec [AUX:TryMakeDateFromStr] '2.10.2015', @Date out, 1; select @Date '2.10.2015'
exec [AUX:TryMakeDateFromStr] N'02.ЙЫ.2015', @Date out; select @Date N'2.ЙЫ.2015'
*/
