The below sql script is from my Crystal report that has one user prompt. The prompt can be (1) the wildcard %, (2) a combination of text and the wildcard % (eg WD%) or (3) the full value for the term being selected (eg 2014FA). I am having difficulty using sql to turn all of these options into the required result. I've messed with this using so many approaches and the one I currently have works for (1) and (3), but I can't get (2) to work.
Please just assume that @MYTERM is the prompt coming from the Crystal prompt. I need to select the latest TERM_END_DATE of all the terms selected like 'WD%' and assign it to @REPORTRUNDATE. I'm pretty sure that what I show for (2) below is way more complicated than necessary -- it yielded no errors, but it also did not yield any values when it should have. What am I missing to get that one date I need for option (2)?
DECLARE @MYTERM AS VARCHAR (100)
SET @MYTERM = 'WD%'
DECLARE @REPORTRUNDATE AS DATE
SET @REPORTRUNDATE =
( CASE
--(1)
WHEN @MYTERM = '%' THEN SYSDATETIME()
--(2)
WHEN (SELECT TOP 1 TT.TERM_END_DATE FROM TERMS TT WHERE TT.TERMS_ID LIKE @MYTERM ORDER BY TT.TERM_END_DATE DESC) IS NOT NULL
THEN (SELECT TOP 1 TTT.TERM_END_DATE FROM TERMS TTT WHERE TTT.TERMS_ID LIKE @MYTERM ORDER BY TTT.TERM_END_DATE DESC)
--(3)
ELSE (SELECT T.TERM_END_DATE FROM TERMS T WHERE T.TERMS_ID = @MYTERM)
END
)
Script continues to load a temporary table based on @REPORTRUNDATE, and the actual report query.
Please just assume that @MYTERM is the prompt coming from the Crystal prompt. I need to select the latest TERM_END_DATE of all the terms selected like 'WD%' and assign it to @REPORTRUNDATE. I'm pretty sure that what I show for (2) below is way more complicated than necessary -- it yielded no errors, but it also did not yield any values when it should have. What am I missing to get that one date I need for option (2)?
DECLARE @MYTERM AS VARCHAR (100)
SET @MYTERM = 'WD%'
DECLARE @REPORTRUNDATE AS DATE
SET @REPORTRUNDATE =
( CASE
--(1)
WHEN @MYTERM = '%' THEN SYSDATETIME()
--(2)
WHEN (SELECT TOP 1 TT.TERM_END_DATE FROM TERMS TT WHERE TT.TERMS_ID LIKE @MYTERM ORDER BY TT.TERM_END_DATE DESC) IS NOT NULL
THEN (SELECT TOP 1 TTT.TERM_END_DATE FROM TERMS TTT WHERE TTT.TERMS_ID LIKE @MYTERM ORDER BY TTT.TERM_END_DATE DESC)
--(3)
ELSE (SELECT T.TERM_END_DATE FROM TERMS T WHERE T.TERMS_ID = @MYTERM)
END
)
Script continues to load a temporary table based on @REPORTRUNDATE, and the actual report query.