How to get the report between certain date range in Cognos - sql-server-2008

I have recently started working on Cognos Report Studio. had a T-sql code with 4 table joins. I simply pasted the code in Cognos Report Studio by dragging the SQL toolbox to Query Explorer. The report did run successfully. But now I want this report generated on 1st of every month with maturity date falling between 1st to 30th/31st of that month.
For Eg: If I get a report on 1st May, It should give records of data where the maturity date range is between 1st may to 31st may.
I tried adding the below code to my already written SQL code:
WHERE
CURR_MATURITY_DATE BETWEEN (DATEADD(MM, 0, GETDATE()), 0) AND (DATEADD(MM, 0, GETDATE()) +1, 0) -1)
This code doesn't work.
Pl Note: THe format of column CURR_MATURITY_DATE is: mm/dd/yyyy. Please advise what changes are required in the code to run successfully.

Cognos has an add months function. (At least, Cognos 10 does.) In your expression editor, on the functions tab, it's under Business Date/Time Functions. It's called _add_months. So your function would end up being something like between (_add_months(1,current_date))
You can also use the SQL Server function. You have to put those text parameters (like MM) in curly brackets. So you would end up with dateadd({MM},0,current_date).
For the first day of the current month, in TSQL you would use:DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
In Cognos, the syntax would be DATEADD({MONTH}, DATEDIFF({MONTH}, 0, GETDATE()), 0)

Try these in your WHERE clause:
For first day of the month: select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
For last day of the month : select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate() )+1, 0))
Find it here, it explains all.

Related

IsWeekEnd Expresion in SSIS that is locale-neutral

How can I write an SSIS expression that determines whether a date is a weekend day?
I have this expression, but it seems it only 'works on my machine'
DATEPART( "Weekday", #[User::CurrentDate] ) ==6 || DATEPART( "Weekday", #[User::CurrentDate] ) ==7
After I deploy SSIS package to server, is suddenly changes order of numbers and
Monday became 2.
When on my computer Monday is 1, exactly as I expected.
I checked SQL Server itself, but it seems is something inside SSIS decide if Sunday or Monday is first day.
I cannot find exact question related to this issue.
Well, if you must do this in an SSIS expression, here is one way to work around the problem: you would compare the DW of your variable to the DW of a known constant (like August 3, 2019, which is a Saturday in any locale that I know of).
In pseudocode, IF ( DW(DateVariable) = DW("20190803") OR DW(DateVariable) = (DW("20190803")+1)%7 ) THEN {DateVariable is a weekend}
Personally, I would look for a way to do this in TSQL. Import the raw data "as is" into a staging table, and then do the transformation while moving the data to the destination table with a stored procedure.
DATEPART("DW",GETDATE()) or DATEPART("WEEKDAY",GETDATE()) works perfectly fine on both SQL server and local.
How are you passing value to #[User::CurrentDate] variable(Please check if same date is set when packages is executed on SQL server and on local)
I changed DateTime on my local and tested your expression as part Of Execute SQL task and Derived Column Transformation which gives me 1 for Sunday, 2 for Monday and so on and the result matches with SQL.
SQL query to get day of week:
SELECT GETDATE() Today,DATENAME("DW", GETDATE()) DayofWeekToday,
DATEPART("DW",GETDATE()) DayNumberToday,GETDATE()-1 Yesterday,
DATENAME("DW", GETDATE()-1) DayofWeekYesterday,DATEPART("DW",GETDATE()-1) DayNumberYesterday
Adding more to my answer : There is a difference between DateFirst and DatePart. DateFirst sets first day of the week while Datepart gives you day of the week. use this sql function to check what's your first day of the week
Set Datefirst 7 -- To set first day of week (7 = Sunday)
SELECT ##DATEFIRST -- To check first day of week
**Update your system settings to match first day of the week with sql server and you should get same values when you evaluate expression.
Your expression looks good its only your first day of week on local and sql server are not matching.
Once I updated first day of week on SQL, I was able to replicate the issue you are facing.

Evaluating parameters in SSRS report heading

Hi I am trying to create a report in SSRS for a survey. The report title should have the survey date displayed. Below is what my report looks like in excel:
It has the survey date in report heading. I am trying to do similar thing in SSRS but not able to display the dates. This is what my report looks like in SSRS:
Sorry I had to blacken some of the fields. Anyways so in SSRS I created two parameters #reportStartDate and #reportNextDate. I want these parameters to be populated with yesterday's date and current date. So the param #reportStartDate should be yesterday's date and #reportNextDate should be today's date.
I tried multiple things and haven't figured out the solution yet. In the dataset properties, I assigned the following expression to the parameters:
#reportStartDate = DATEADD (DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
#reportNextDate = DATEADD (DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
Here is how I created the parameters:
And here is how I assigned the parameters with expression in Dataset properties:
But after this when I preview the report, I get the error
I am working on SSRS for first time and have been struggling with this issue from almost more than a day. Any idea what I am missing or doing wrong?
Add a new dataset that contains the following
select DATEADD (DAY, DATEDIFF(DAY, 0, GETDATE()), -1) as startdate,
DATEADD (DAY, DATEDIFF(DAY, 0, GETDATE()), 0) as enddate
You should then be able to access these fields as internal in the parameter select screenReport data will look like this:
Make the parameter visibility to internal as you don't want the user to see it.
Get values from the new dataset and repeat the process for end date
I know you have accepted an answer but unless I'm mistaken it looks like a simple case of your title expression being wrong.
Your title expression should be something like
="Hotel Mobile Debrief Detail Report from " & Parameters!reportStartDate.Value & " to " & Parameters!reportNextDate.Value
There is no need to add datasets simply to display the parameter values in a text box.

Last sunday of previous week in SSIS

I want a SSIS expression which will give me the sunday date of last week.
Corresponding sql server query looks like :
select convert(date,DATEADD(wk, DATEDIFF(wk, 6, convert(date,#report_dt)), -1))
I want the same result in SSIS expression.
Thanks
Assuming your date is held in a variable called report_dt, this works for me:
(DT_DATE)LEFT((DT_WSTR, 30)DATEADD("Day", -1 * (DATEPART("Weekday", #[User::report_dt]) + 7), #[User::report_dt]), 10)
However not sure if this is region safe - but it works in the UK.
This will give you the Sunday of last week using the variable report_dt:
(DT_DBDATE)DATEPART("dw",#[User::report_dt]) == (DT_DBDATE)1 ? #[User::report_dt] : DATEADD("day",-(DATEPART("dw",#[User::report_dt]) - 1),#[User::report_dt])
It's casted as DT_DBTIME so only the date is returned.

SQL Server Reporting Services Date/Sum weirdness

I have 2 columns next to each other in a report I'm building. They contain the same structure, with one small difference. One column looks back in time a little further then the other. I have verified that the data is there to be found. A row group below returns the rows using the same structure. Column A returns the correct data. Column B does not.
Column A Expression
=IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") > DateAdd("d", -31, Now()), Sum(Fields!Value.Value), 0)
Column B Expression
=IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") >= DateAdd("d", -61, Now()), Sum(Fields!Value.Value), 0)
I've tried every single variation I can find, and nothing returns the correct data. I'm on SQL Server 2012. Thanks for any help.
Put your IF inside the SUM instead of the other way around.
Something like this should work. You'll want to change both the 30 day and 60 day expressions to fit this pattern.
Sum(
IIF(Format(Fields!IncomingInvoiceDate.Value, "yyyy-MM-dd") >= DateAdd("d", -61,Now()), Fields!Value.Value, 0)
It turns out nothing has worked, and I redesigned the query completely. Thanks all.

Comparing todays date against first day of the week

I am trying to set a default parameter value in SSRS report. I want to test the current date to see if it equals the first day of the week (in my case Monday). If it is the first day of week, then I want the default value to be current date minus 2 days, if it is not the first day of the week then I want the default value to be current date minus 1 day.
I seem to have a syntax problem but it doesn't tell me where. My parameters are StartDate and EndDate.
this is what I've tried:
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today(),DateAdd("d",-1,today())
this is the generic error I get:
The value expression for the report parameter 'StartDate' contains eror:[BC30201] Expression expected.
Where am I going wrong?
You are trying to use Sql syntax in a SSRS VBA expression. SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum.
So your expression needs to use VBA syntax:
=IIF(Weekday(Today, FirstDayOfWeek.Monday) = 1, DateAdd(DateInterval.Day, -2, Today), DateAdd(DateInterval.Day, , -1, Today))
It appears that you are missing a closing parentheses after the first logical part of the if statement and another to close the statement.
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today()),DateAdd("d",-1,today()))