-- rmDateDecode([Date]}
-- derived from DateDecoder.sql by Tom Holden ve3meo https://sqlitetoolsforrootsmagic.com/date-decoder/
CASE Substr( :rmDate , 1 , 1 )
	WHEN 'Q' THEN :rmDate 
	WHEN 'T' THEN Substr( :rmDate , 2 ) 
	WHEN 'D' THEN 
		CASE Substr( :rmDate , 2 , 1 ) 
			WHEN 'A' THEN 'aft ' 
			WHEN 'B' THEN 'bef ' 
			WHEN 'F' THEN 'from ' 
			WHEN 'I' THEN 'since ' 
			WHEN 'R' THEN 'bet ' 
			WHEN 'S' THEN 'from ' 
			WHEN 'T' THEN 'to ' 
			WHEN 'U' THEN 'until ' 
			WHEN 'Y' THEN 'by ' 
		ELSE '' 
		END 
		|| 
		CASE Substr( :rmDate , 13 , 1 ) 
			WHEN 'A' THEN 'abt ' 
			WHEN 'C' THEN 'ca ' 
			WHEN 'E' THEN 'est ' 
			WHEN 'L' THEN 'calc ' 
			WHEN 'S' THEN 'say ' 
			WHEN '6' THEN 'cert ' 
			WHEN '5' THEN 'prob ' 
			WHEN '4' THEN 'poss ' 
			WHEN '3' THEN 'lkly ' 
			WHEN '2' THEN 'appar ' 
			WHEN '1' THEN 'prhps ' 
			WHEN '?' THEN 'maybe ' 
		ELSE '' 
		END 
		|| 
		CASE WHEN Substr( :rmDate , 3 , 1 ) = '-' THEN 'BC' ELSE '' END  
		|| 
		CASE WHEN Substr( :rmDate , 4 , 4 ) = '0000' THEN '' ELSE Substr( :rmDate , 4 , 4 ) END 
		|| 
		CASE WHEN Substr( :rmDate , 12 , 1 ) = '/' THEN '/' || ( 1 + Substr( :rmDate , 3 , 5 ) ) ELSE '' END 
		|| 
		CASE 
			WHEN Substr( :rmDate , 8 , 2 ) = '00' AND ( Substr( :rmDate , 4 , 4 ) <> '0000' AND Substr( :rmDate , 10 , 2 ) <> '00' ) THEN '-??' 
			WHEN Substr( :rmDate , 8 , 2 ) = '00' AND ( Substr( :rmDate , 4 , 4 ) <> '0000' AND Substr( :rmDate , 10 , 2 ) == '00' ) THEN '' 
			WHEN Substr( :rmDate , 8 , 2 ) = '00' THEN '' 
		ELSE '-' || Substr( :rmDate , 8 , 2 ) 
		END 
		|| 
		Coalesce( Nullif( '-' || Substr( :rmDate , 10 , 2 ) , '-00' ) , '' ) 
		|| 
		CASE Substr( :rmDate , 2 , 1 ) 
			WHEN 'R' THEN ' and ' 
			WHEN 'S' THEN ' to ' 
			WHEN 'O' THEN ' or ' 
			WHEN '-' THEN ' - ' 
		ELSE '' 
		END 
		|| 
		CASE Substr( :rmDate , 24 , 1 ) 
			WHEN 'A' THEN 'abt ' 
			WHEN 'E' THEN 'est ' 
			WHEN 'L' THEN 'calc ' 
			WHEN 'C' THEN 'ca ' 
			WHEN 'S' THEN 'say ' 
			WHEN '6' THEN 'cert ' 
			WHEN '5' THEN 'prob ' 
			WHEN '4' THEN 'poss ' 
			WHEN '3' THEN 'lkly ' 
			WHEN '2' THEN 'appar ' 
			WHEN '1' THEN 'prhps ' 
			WHEN '?' THEN 'maybe ' 
		ELSE '' 
		END 
		|| 
		CASE WHEN Substr( :rmDate , 14 , 1 ) = '-' THEN 'BC' ELSE '' END 
		|| 
		CASE WHEN Substr( :rmDate , 15 , 4 ) = '0000' THEN '' ELSE Substr( :rmDate , 15 , 4 ) END 
		|| 
		CASE WHEN Substr( :rmDate , 23 , 1 ) = '/' THEN '/' || ( 1 + Substr( :rmDate , 14 , 5 ) ) ELSE '' END 
		|| 
		CASE 
			WHEN Substr( :rmDate , 19 , 2 ) = '00' AND ( Substr( :rmDate , 15 , 4 ) <> '0000' AND Substr( :rmDate , 21 , 2 ) <> '00' ) THEN '-??' 
			WHEN Substr( :rmDate , 19 , 2 ) = '00' AND ( Substr( :rmDate , 15 , 4 ) <> '0000' AND Substr( :rmDate , 21 , 2 ) == '00' ) THEN '' 
			WHEN Substr( :rmDate , 19 , 2 ) = '00' THEN '' 
		ELSE '-' || Substr( :rmDate , 19 , 2 ) 
		END 
		|| 
		Coalesce( Nullif( '-' || Substr( :rmDate , 21 , 2 ) , '-00' ) , '' ) 
	ELSE '' 
END