--:SetVar Procedure "AUX:TryMakeDateFromStr"
if exists( select * from [sysobjects] (nolock) where [type]='P' and [name]=N'AUX:TryMakeDateFromStr' )
   drop procedure [AUX:TryMakeDateFromStr]
GO
/* Translated by $SQLTRANS utility (SQL-file technology). Method author: Sergei Y.Kitáev.
** Script translation date-time: 2022-11-27 00:29:29.24 (stamp of SQL-file generation). */
create procedure [AUX:TryMakeDateFromStr] @DateString              nvarchar(max),
                                @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 begin try set xact_abort off; set ansi_nulls,ansi_null_dflt_on,quoted_identifier,arithabort,nocount on; set implicit_transactions off; declare @initial_tran_count_aux int, @is_local_tran_open_aux bit; select @initial_tran_count_aux=@@trancount, @is_local_tran_open_aux=0; end try begin catch throw; end catch; begin try
----------------------------------------------------------------------------------------------------

if @DateString is null
   exec [AUX:RaiseIncorrectProcedureCall] 'AUX:TryMakeDateFromStr'

----------------------------------------------------------------------------------------------------
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

----------------------------------------------------------------------------------------------------
if @is_local_tran_open_aux=1 raiserror ('Not closed local transaction was detected, previously opened by BEGIN_TRANSACTION_AUX macro.',11,1); end try begin catch if @is_local_tran_open_aux=1 and (@@trancount>@initial_tran_count_aux or xact_state()=-1) rollback transaction; set @is_local_tran_open_aux=0; throw; end catch; end
/*================================================================================================*/
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'
*/
