﻿
CREATE procedure [dbo].[CRT_BreakDownUnitDefinition]
		@UnitDefinition varchar(15),
		@Year int output,
		@Month int output,
		@Week int output,
		@Day int output,
		@Hour int output,
		@Minute int output
as

set @Year = null
set @Month = null
set @Week = null
set @Day = null
set @Hour = null
set @Minute = null

declare @i_UnitDefinition varchar(16),
		@i int,
		@CurrentChar varchar(1),
		@CurrentValue varchar(2),
		@CurrentUnit char(1)

set @i_UnitDefinition = @UnitDefinition + 'E'

set @i = 1

while @i <= len(@i_UnitDefinition)
begin
	set @CurrentChar = substring(@i_UnitDefinition, @i, 1)

	if isnumeric(@CurrentChar) = 1
		set @CurrentValue = @CurrentValue + @CurrentChar
	else
	begin
		if @CurrentValue <> ''
		begin
			if @CurrentUnit = 'N'
				set @Minute = cast(@CurrentValue as int)
			else if @CurrentUnit = 'H'
				set @Hour = cast(@CurrentValue as int)
			else if @CurrentUnit = 'D'
				set @Day = cast(@CurrentValue as int)
			else if @CurrentUnit = 'W'
				set @Week = cast(@CurrentValue as int)
			else if @CurrentUnit = 'M'
				set @Month = cast(@CurrentValue as int)
			else if @CurrentUnit = 'Y'
				set @Year = cast(@CurrentValue as int)
		end
		set @CurrentUnit = @CurrentChar
		set @CurrentValue = ''
	end
	set @i = @i + 1
end