Using case when in Cognos data item - cognos-10

I have put this in the Data Filter expression and it causes error when running the report. What I'm trying to achieve here is that when the Force Expired column has a value of Force then the action on the two other columns such as ([Policy Expiration Date] and [Policy Effective Date] should use the logic used against the current_date.
case when [Force Expired] = 'Inforce' then ([Policy Expiration Date] >= current_date and [Policy Effective Date] <= current_date) else end

Cognos is finicky about the exact syntax of conditionals in filters. I generally avoid CASE statements in filters for this reason. You can convert your filter to one of these two syntaxes instead:
IF ([Force Expired] = 'Inforce')
THEN ([Policy Expiration Date] >= current_date and [Policy Effective Date] <= current_date)
ELSE (1=1)
([Force Expired] = 'Inforce' AND [Policy Expiration Date] >= current_date AND [Policy Effective Date] <= current_date)
OR
[Force Expired] <> 'Inforce'
If you insist on using a CASE statement then I believe you can put parentheses around your WHEN clause and provide a result for ELSE and it should work:
CASE
WHEN ([Force Expired] = 'Inforce')
THEN ([Policy Expiration Date] >= current_date AND [Policy Effective Date] <= current_date)
ELSE (1=1)
END
For some reason, Cognos insists on an ELSE clause.

when you use case you have to specify what to do when the requirement is met - not to give another condition.
something like:
case when [bla] = "foo" then "return_value" else "nothing" end
remember -whatever you write in a calculated vlue in a data item is translated to sql, and that's precisely how it also goes in sql...

Related

Why does to_char() always convert minutes to 07?

v_dateTime := sysdate;
update products
set received_dt = v_dateTime
where id = v_id
and received_dt is null;
dbms_lob.createtemporary(po_data, true);
po_data := to_clob('{"receivedDateTime": "' || to_char(v_dateTime, 'dd.mm.yyyy hh24:mm:ss') || '"}');
The result of executing this code is a correct datetime value inserted into the table. However, the to_char() function always converts minutes to '07'. The rest of the date and time are correct, but minutes are always changed to 07. Why??
As documented in the manual, the format mask for minutes is mi - mm is the month.
You need to use:
to_char(v_dateTime, 'dd.mm.yyyy hh24:mi:ss')

Comparing date/time values in microsoft access 2010

I am having some trouble comparing date/time values in Microsoft Access.
I am currently using the query below to get all the absence records for the day. I have no issues when it is a plain date value but records with time included cannot be retrieved with the query.
SELECT * FROM table_name
WHERE [Start Date/Time] <= Date() AND [End Date/Time] >= Date()
I have a table used to store absence records of the following form.
Name: Text
Start Date/Time: Date/Time
End Date/Time: Date/Time
You can for example use:
SELECT * FROM table_name
WHERE Fix([Start Date/Time]) <= Date() AND Fix([End Date/Time]) >= Date()
to remove the time part, or:
SELECT * FROM table_name
WHERE DateDiff("d", [Start Date/Time], Date()) >= 0 AND DateDiff("d", [End Date/Time], Date()) <= 0
to ignore the time part.

How do I get the value of the oldest date from a transaction table using a data item in Cognos 10 Report Studio?

How do I apply this query in Cognos Report Studio? Im thinking of creating a Data Item. Both queries are just the same, created in two different approach.
SELECT [Transaction Date], Amount
FROM DW.AmountTable
WHERE [Transaction Date] IN (SELECT Min([Transaction Date]) FROM DW.AmountTable)
SELECT A.[Transaction Date], A.[GWP Amt] [Amount]
FROM DW.AmountTable A
INNER JOIN (SELECT min(transaction date) MTD, [Policy Number])
FROM dw.amountTable
GROUP BY [Policy Number]) B
on B.MTD=A.[Transaction Date]
and A.[Policy Number] = B.[Policy Number]
where A.[Policy Number] = '7030500'
Should I create a separate Data Item for minimum(Transaction Date)?
Should I also create a new Data Item for the Amount that contains the Data Item for minimum(Transaction Date)?
The two queries you list do not do the same thing.
The first will return the earliest date for the entire table and only show rows that match that date.
The second query will fetch the earliest date for each policy number, join the main table on that date and then filter the results to only show policy number '7030500'.
To replicate the first query use this filter:
[Transaction Date] = minimum([Transaction Date] for report)
To replicate the second query use this filter:
[Transaction Date] = minimum([Transaction Date] for [Policy Number])
AND
[Policy Number] = '7030500'

ASP.Net SQL Where Date is after Today

Hi all I have the following Query made using the Query Builder in Visual Studio.
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID)
I want to add another WHERE statement which adds where Schd_Date is after todays date, any ideas?
Assuming SQL Server, the GETDATE() function returns the date and time the statement was run at:
WHERE Schd_Date > GETDATE()
Use the following for finding dates greater than the current date at midnight:
WHERE Schd_Date > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
However, CURRENT_TIMESTAMP is the ANSI means of getting the current date and time in a database. Beyond that, date functionality is not consistent between databases so you'd have to tell us what you are dealing with for better answers.
If this is SQL Server you could use the GETDATE() function to return the current date and compare against it:
SELECT
Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM
Schedule
WHERE
(Schd_Avaliable = 'Yes')
AND
(Accom_ID = Accom_ID)
AND
(Schd_Date > GETDATE())
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND (GETDATE() < Schd_Date)
This should work
I guess this expression can be used as a date representing the current date on the format yyyy-mm-dd (without the hours,minutes and seconds).
(Datepart('yyyy',getdate())+ '-' +Datepart('m',getdate())+ '-' + Datepart('d',getdate()))
therefore
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND
(Schd_Date >= (Datepart('yyyy',getdate())+ '-' +
Datepart('m',getdate())+ '-' +
Datepart('d',getdate())))

SQL Date Search without time

I have a query that searches by date. the dates in the database include the time. How do I search on just the date only.
select *
from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and (client_user=#UserName or #UserName Is null)
order by Log_time desc
cmd.Parameters.AddWithValue("#BeginDate", txtBeginDate.Text);
cmd.Parameters.AddWithValue("#EndDAte", txtEndDate.Text);
Leave your sql mostly as is and just fix your parameters:
cmd.Parameters.Add("#BeginDate", SqlDbType.DateTime).Value =
DateTime.Parse(txtBeginDate.Text).Date;
cmd.Parameters.Add("#EndDAte", SqlDbType.DateTime).Value =
// add one to make search inclusive
DateTime.Parse(txtEndDate.Text).Date.AddDays(1);
You also want to check to make sure your textboxes are valid datetimes first, but you should get the idea.
The only caveat here is that due to a quirk with the BETWEEN operator it will match the first instant of the next day. So, to fix that we write the query like this:
SELECT *
FROM vwlogs
WHERE Log_time >= #BeginDate AND Log_Time < #EndDate
AND (client_user=#UserName OR #UserName IS NULL)
ORDER BY Log_time DESC
Pay special attention to the comparision operators around the date.
The first thing to do is to remove the times from the dates. If you want to do this in the sql server code you can use something like the code below. I have this as a function on all the databases I work on
cast(floor(cast(#fromdate as float)) as datetime)
The next thing to worry about is the where criteria. You need to make sure you select everything from the start of the from date to the end of the to date. You also need to make sure queries for one day will work which you can do with a date add like this
Where LogTime >= #fromdate and LogTime < DateAdd(dd, 1, #todate)
In SQL round the start and end date to Whole Dates and use >= #BeginDate and very specifically < #EndDAte. The "rounding" process is not very elegant I'm afraid
e.g.
SELECT #BeginDate = DATEADD(Day, DATEDIFF(Day, 0, #BeginDate), 0),
#EndDAte = DATEADD(Day, DATEDIFF(Day, 0, #EndDAte) + 1, 0)
select *
from weblogs.dbo.vwlogs
where Log_time >= #BeginDate
and Log_time < #EndDAte
and (#UserName Is null OR client_user=#UserName)
order by Log_time desc
Note that I've moved "#UserName Is null" first, as there is some evidence that this test will easily pass/fail, and will cause the second more CPU intensive test (client_user=#UserName) to be ignored if the first test is TRUE (may be TommyRot of course ...)
Also, for best performance, you should explicitly name all the columns you need, and not use "SELECT *" (but that may just have been for the purpose of this question)
If you want to change the sql instead,
TRUNC(Log_Time) will reduce every datetime to to that date at midnight.
Make sure that you build your index on the column as TRUNC(Log_TIME) so it's usable.
Another gotcha - truncating your end date will NOT include that date! Consider:
WHERE Log_Time >= #BeginDate AND Log_Time < #EndDate
If #EndDate is truncated it will be midnight and not match anything on that day. You'll need to add a day!
Clean up the dates by adding the following line before your query...
select
#begindate=dateadd(day,datediff(day,0,#begindate),0),
#enddate=dateadd(ms,-3,dateadd(day,datediff(day,0,#enddate),1))
This will floor your begin date to the lowest possible time (00:00:00.000), and ceiling your end date to the highest possible (23:59:59.997). You can then keep your BETWEEN query exactly as it was written.
select *
from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and (client_user=#UserName or #UserName Is null)
order by Log_time desc
Hope this helps.

Resources