CREATE OR ALTER PROCEDURE PRIDB_SEARCH_TEXT_GET_RESULTS (@pUSR_ID as INTEGER,@pSEARCH_TERM as VARCHAR(900),@pDATE_RESTRICTION VARCHAR(500), @pLANG_CODE as VARCHAR(5) = 'de-DE')
AS
BEGIN
	DECLARE @SRCH_VAR VARCHAR(900),	@SQL NVARCHAR(MAX),@DateStr VARCHAR(100)
	IF @pSEARCH_TERM like '%*%'
		SET @SRCH_VAR = ' "' + @pSEARCH_TERM + '" '
	ELSE
		SET @SRCH_VAR = @pSEARCH_TERM
	PRINT '@SRCH_VAR [' + @SRCH_VAR + ']'
	DELETE FROM TBIDB_SEARCH_RESULT_USER WHERE USR_ID = @pUSR_ID;

	IF LEN(@pDATE_RESTRICTION) > 0
		BEGIN
			PRINT '@pDATE_RESTRICTION....'
			DECLARE @IDX TINYINT, @ATTR_ID VARCHAR(100)
			SET @IDX = CHARINDEX('~',@pDATE_RESTRICTION)
			PRINT '@IDX: ' + CONVERT(VARCHAR(10),@IDX)
			SET @ATTR_ID = LEFT(@pDATE_RESTRICTION,@IDX-1)
			PRINT CONVERT(VARCHAR(10),@ATTR_ID)
			SET @DateStr = SUBSTRING(@pDATE_RESTRICTION,@IDX + 1,LEN(@pDATE_RESTRICTION)-@IDX)
			PRINT @DateStr
		END

	SET @SQL = 'INSERT INTO TBIDB_SEARCH_RESULT_USER(MD_GUID,USR_ID)
	select MD.GUID,' + CONVERT(VARCHAR(10),@pUSR_ID) +  char(10) +
	'FROM TBIDB_TERM_VALUE_VARCHAR TERM
	INNER JOIN TBIDB_LANGUAGE_TERM TLANG ON TERM.GUID = TLANG.TERM_ID
	INNER JOIN TBIDB_LANGUAGE LANG ON TLANG.LANG_ID = LANG.GUID
	INNER JOIN TBIDB_OBJECT_METADATA MD ON TLANG.OBJECT_ID = MD.GUID
	INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
	WHERE 
	OBJ.ACTIVE = 1' + char(10)
	IF @DateStr = 'DATEPART'
		BEGIN
			DECLARE @DATE1 DATE,@DATE2 DATE
			SELECT @DATE1 = DATE_FROM ,@DATE2 = DATE_TILL FROM TBIDB_SEARCH_DATE_VALUE_USER WHERE USR_ID = @pUSR_ID;
			SET @SQL += 'AND CONVERT(DATE,OBJ.ADDED_WHEN) >= CONVERT(DATE,''' + CONVERT(VARCHAR(10),@DATE1) + ''') AND CONVERT(DATE,OBJ.ADDED_WHEN) <= CONVERT(DATE,''' + CONVERT(VARCHAR(10),@DATE2) + ''')' + char(10)
		END
	ELSE IF @DateStr = 'ECM_CREATED_TODAY'
		SET @SQL += 'AND DATEDIFF(DAY,OBJ.ADDED_WHEN,GETDATE()) = 0' + char(10)
	ELSE IF @DateStr = 'ECM_CREATED_YESTERDAY'
		SET @SQL += 'AND DATEDIFF(DAY,OBJ.ADDED_WHEN,GETDATE()) = 1' + char(10)
	ELSE IF @DateStr = 'ECM_CREATED_LAST7DAYS'
		SET @SQL += 'AND DATEDIFF(DAY,OBJ.ADDED_WHEN,GETDATE()) <= 7' + char(10)
	ELSE IF @DateStr = 'ECM_YEAR_CURRENT'
		SET @SQL += 'AND DATEPART(YEAR,OBJ.ADDED_WHEN) = DATEPART(YEAR,GETDATE())' + char(10)
	ELSE IF @DateStr = 'ECM_YEAR_LAST'
		SET @SQL += 'AND DATEPART(YEAR,OBJ.ADDED_WHEN) = DATEPART(YEAR,GETDATE())-1' + char(10)
	ELSE IF @DateStr = 'ECM_CREATED_MONTH_CURRENT'
		BEGIN
			SET @SQL += 'AND DATEPART(YEAR,OBJ.ADDED_WHEN) = DATEPART(YEAR,GETDATE())' + char(10)
			SET @SQL += 'AND DATEPART(MONTH,OBJ.ADDED_WHEN) = DATEPART(MONTH,GETDATE())' + char(10)
		END
	ELSE IF @DateStr = 'ECM_CREATED_MONTH_LAST'
		BEGIN
			IF DATEPART(MONTH,GETDATE()) = 1
				BEGIN
					SET @SQL += 'AND DATEPART(YEAR,OBJ.ADDED_WHEN) = DATEPART(YEAR,GETDATE())-1' + char(10)
					SET @SQL += 'AND DATEPART(MONTH,OBJ.ADDED_WHEN) = 12' + char(10)
				END
			ELSE
				BEGIN
					SET @SQL += 'AND DATEPART(YEAR,OBJ.ADDED_WHEN) = DATEPART(YEAR,GETDATE())' + char(10)
					SET @SQL += 'AND DATEPART(MONTH,OBJ.ADDED_WHEN) = DATEPART(MONTH,GETDATE())-1' + char(10)
				END
		END


	IF LEN(@SRCH_VAR) > 0
		BEGIN
			IF @pSEARCH_TERM like '%*%'
				SET @SQL += 'AND CONTAINS(TERM.TERM_VALUE,''' + @SRCH_VAR + ''')' + char(10)
			ELSE
				SET @SQL += 'AND TERM.TERM_VALUE = ''' + @SRCH_VAR + '''' + char(10)
		END
	
	SET @SQL +=  'AND LANG.LANG_CODE = ''' + @pLANG_CODE + ''''
	PRINT @SQL
	EXEC sp_executesql @SQL
END
GO
EXEC  PRIDB_SEARCH_TEXT_GET_RESULTS 1,'strun*','' --doct*
GO

SELECT 
	DOC.*
FROM 
	TBIDB_SEARCH_RESULT_USER RES INNER JOIN VWIDB_LANGUAGE_TERM VWLTERM ON RES.MD_GUID = VWLTERM.MD_ID
	INNER JOIN VWIDB_DOCRESULT_DE DOC ON VWLTERM.IDB_OBJ_ID = DOC.DocID
WHERE
	RES.USR_ID = 1 AND VWLTERM.LANG_CODE = '@LANG_CODE'



--' "doct*" 
EXEC PRIDB_SEARCH_TEXT_GET_RESULTS 1,'','ADDED_WHEN~ECM_CREATED_MONTH_CURRENT'

select * from TBIDB_OBJECT order by IDB_OBJ_ID desc

CREATE TABLE TBIDB_SEARCH_DATE_VALUE_USER
(
	USR_ID		INTEGER
	,DATE_FROM	DATE		NOT NULL
	,DATE_TILL	DATE
	
	CONSTRAINT PK_TBIDB_SEARCH_DATE_VALUE_USER	PRIMARY KEY (USR_ID)
)
GO

CREATE PROCEDURE PRIDB_SEARCH_ADD_USR_DATE @USERID INT,@DATE1 DATE, @DATE2 DATE
AS
BEGIN
	DELETE FROM TBIDB_SEARCH_DATE_VALUE_USER WHERE USR_ID = @USERID;
	INSERT INTO TBIDB_SEARCH_DATE_VALUE_USER (USR_ID,DATE_FROM,DATE_TILL) VALUES
	(@USERID,@DATE1,@DATE2);

END