Using IS NOT NULL and IS NULL as parameters in SSRS? - sql-server

I am creating an SSRS report where I need to have a parameter called Status and the values would be "Current", "Former", or basically selecting both of them in terms of display both Current and Former.
This parameter will reference a date column and if the (date is NULL OR date > GETDATE()) then they would be considered "Current"
If (date is not NULL AND date < GETDATE()) then they would be considered "Former"
I've tried using in the WHERE clause (WHERE DATE #NULL OR DATE > GETDATE()) and similarly with the "Former" parameter value, but I am not sure if in SSRS if it's possible to pass the IS NULL and IS NOT NULL to these parameter values.
Sample of table data:
Name Date
John NULL
Bill 2/27/2021
Bob 2/1/2021
So in this example, John is "Current", Bill is still "Current", and Bob is "Former".

I think what you're looking for is this:
WHERE (#param = 'Current' AND (Date is NULL OR date > GETDATE()))
OR (#param = 'Former' AND Date <= GETDATE())
OR #param = 'All'
As jarlh said, you don't need to check for "is not null and less than" in the second line.
Not sure what you meant by "basically selecting both of them" so I assumed a third option for "all". If the parameter being null triggers that instead, you should be able to change the last line to:
OR #param is NULL
Then "NULL IS NULL" would satisfy the third condition and bring back all rows.

Related

How do I only update null values in a column with SOQL?

I have an issue with SOQL within a query activity in Salesforce Marketing Cloud Automation Studio.
I want to update null values with birth dates of users in a column named "date of birth" that includes birth dates of a ton of users with datatype = date.
I want to update this column, "date of birth", based on another column named "date of birth 2" which also includes birth dates of a ton of users but instead with datatype = string.
Thus, my goal is to be able to update only the missing date fields in the column "date of birth" leveraging data from the column "date of birth 2".
With my current very simple code (see img 1/2), I either update nothing in the data extension (when the WHERE column = NULL is added, see img 2) or I update the null values but also overwrite the existing date values in the data extension (when there's not added a WHERE column = NULL, see img 1). I wonder if it possible to simply update the null fields and let the currently filled fields stay?
Looking forward to hearing from you and please tell me if my query is unclear!
So I think you can query on the object where your (date of birth) field is equal to null and (date of birth 2) is not equal null.
SELECT (date of birth),(date of birth 2) FROM objectName WHERE (date of birth) = NULL AND (date of birth 2) != NULL
Please make sure your (date of birth 2) field must be in YYYY-MM-DD format.
Sample code.
List<objectName> li = [SELECT (date of birth),(date of birth 2) FROM objectName WHERE (date of birth) = NULL AND (date of birth 2) != NULL];
for(objectName o : li){
(date of birth) = Date.valueOf((date of birth2));
}
update li;

Get all dates greater than current date in SSRS

I have two parameters in my Report - Begin and End Dates which are of Date/Time type. When I select the Begin Date from the Calendar, how do I automatically show dates equal to or greater than Begin Date for the End Date calendar? I would like the have the user the ability to select only End dates after Begin date.
This could be a potential workaround going off the comment I made to your question. You would need three datetime parameters:
-#1 StartDate
-#2 EndDate
-#3 EndDate2 - make this hidden
The StartDate and EndDate (#1 and #2) are for users to select. For the report, we will use StartDate (#1) and EndDate2 (#3) to filter.
You can set the default for StartDate/EndDate however you would like. For EndDate2, if the user selects a date prior to the start date, default the end date that gets passed to the report. We set this by adjusting the default value for EndDate2 parameter with the IIF function to compare the user input dates.
Add this to default value for EndDate2 parameter:
=IIF(Parameters!EndDate.Value < Parameters!StartDate.Value, Parameters!StartDate.Value, Parameters!EndDate.Value )

can we use if condition for compare 2 dates and display the row in sql server

I have 2 dates column, one is current date and the one is user define date, I have to display the specific row when the user define date is 2days greater than current date.
In the below code I try to display the row, when both dates are equal. But I don't know how display the row in sql.
I have following Columns,
Rid
DateTime (User define date and time)
Reminder
Description
CRDateTime (current date and time)
I have to set 3 condition.
if both dates are equal means that should be display,
if DateTime is 2 days before CRDate means I have to display that row,
if DateTime is 1 day before the CRdate means I have to display that row.
declare DateTime as datetime1;
select RId if CAST (DateTime as date) =CAST (#CRDateTime as date)
The IF...ELSE statement is a control-flow statement that allows you to execute or skip a statement block based on a specified condition.
For example:
IF Boolean_expression
BEGIN
-- Statement block executes when the Boolean expression is TRUE
END
ELSE
BEGIN
-- Statement block executes when the Boolean expression is FALSE
END
If you want to check something in your SQL Statements you should use the where clause.
It could be looking like:
SELECT * FROM `[YOUR_TABLE_NAME]` WHERE `DateTime` = `CRDateTime`
If you want to check the date in a 2 Day decade you can use the SQL DATEADD Function.
It lookse like:
SELECT DATEADD(day, +1, '2017/08/25') AS DateAdd;
Resault of this code is 2017/08/26
In your case it looks like this:
SELECT RId where DATEADD(day, +2, DateTime) = CRDateTime
--- EDIT SECTION: ---
EDIT 1:
All in one you can use this code here:
SELECT RId WHERE
`DateTime` = `CRDateTime` OR
DATEADD(day, +1, DateTime) = CRDateTime OR
DATEADD(day, +2, DateTime) = CRDateTime
As I understand you basically want to show all the records with in range of 2 days.
SELECT RId, DateTime, Reminder, Description, CRDateTime
WHERE CRDateTime BETWEEN DATETIME AND DATEADD(day, +2, DateTime)
Or you may try this
SELECT RId, DateTime, Reminder, Description, CRDateTime
WHERE DateTime BETWEEN DATEADD(day, -2, CRDateTime) AND CRDateTime
The question isn't clear. I assume the actual question is how to filter a table's rows between two dates.
Filtering in SQL (in any product) is the job of the WHERE clause, not IF. You can use the BETWEEN clause to select values in a range.
If both the table field and the query parameter are date variables, the query is easy :
CREATE TABLE table1
(
RId int PRIMARY KEY,
DateField date,
INDEX IX_Table1_Date (DateField)
)
declare #dateParam date='20190801';
SELECT RId
FROM table1
WHERE DateField BETWEEN dateadd(day,-2,#dateParam) AND #DateParam
This query will take advantage of the IX_Table1_Date index to speed up the search. Typically, applying any kind of function on a table field prevents the query engine from using any index that includes that field simply because the values stored in the index have no relation to the function's result.
If you use the date parameter to be the current date, just assign GETDATE() to it.
declare #dateParam date=GETDATE();
If the field isn't a date, you can cast it to date and still get a fast range query, because the query engine is fast enough to convert the cast to a range query.
SELECT RId
FROM table1
WHERE cast(DateField as date) BETWEEN dateadd(day,-2,#dateParam) AND #DateParam
If DateField is not a date-related type, eg it's a varchar, a) that's a serious bug and b) the server won't be able to use any indexes.
Casting the parameter values won't affect performance as they actual values are calculated before the query starts executing. The query becomes quite noisy though :
declare #dateParam datetime='20190801'
SELECT RId
FROM table1
WHERE cast(DateField as date)
BETWEEN dateadd(day,-2,cast(#dateParam as date)) AND cast(#DateParam as date)
That's why it's better to use the correct type for parameters

SSRS expression to fill back ground color based on perticuler date

I have this query which will return a date from SQL Server
SELECT TOP 1
CASE
WHEN (SELECT TOP 1 CONVERT(DATE, [Duedate]) AS [Duedate]
FROM [dbo].[Table1]
WHERE CONVERT(DATE, [Duedate]) = CONVERT(DATE, GETDATE())) = CONVERT(DATE, GETDATE())
THEN CONVERT(DATE, GETDATE())
ELSE (SELECT TOP 1 CONVERT(DATE, [Duedate])
FROM [dbo].[Table1]
WHERE CONVERT(DATE, [Duedate]) > CONVERT(DATE, GETDATE())
ORDER BY CONVERT(DATE, [Duedate]) ASC)
END AS [ReturnRequiredDate]
FROM
[dbo].[Table1]
However, when I'm trying to add this to my dataset in a SSRS report, it's returning Date and Time format. How to resolve this?
Secondly, I would like to fill background color of table columns/rows based on this returned date (i.e. previousdays & including returned date).
That is, based on case,if the date returning 16th of any month then the filled color should be, starting from 16th to previous days.
Also, lets say, if the date returned is the 27th of any month then color should be filled starting from 27th back to previous records (previous date records)
How can I do this using SSRS expression?
Using query, out of all the dates, I'm able to return all dates previous to [ReturnRequiredDate]. Is there any ways to fill color to those returned dates only?In the same SSRS table (where all my records are there). I don't want a separate table in the SSRS for color fill.
Hope I have explained the rule clearly, if not please let me know to modify the question. Thanks.
First question
What is the issue with the date being returned as datetime? If you need to do calculcations against it, 2019-05-17 is functionally equivalent to 2019-05-17 00:00:00.0000000. If you need to display it as just a date, use formatting in your report, either in the textbox properties or in your expression, for example: =format(Fields!ReturnRequiredDate.Value,"yyyy-MM-dd")
Second question
Again you just need to apply a data driven expression to your textbox background colour property containing the data that checks if the value is less than the parameter date:
=iif(Fields!YourDate.Value <= first(Fields!ReturnRequiredDate.Value,"YourDataset"),"Red","Black")

date between query issue in sqlite3

In my data base , i was stored one student data in the following date format is 07-03-2013(dd-mm-yyyy). For example the start date greater than the end date ,no row return in the results.
For Ex.
select student_name from general_details where join_date between '11-02-2013' and '08-03-2013'
it returns 0
if start date less than the end date
select student_name from general_details where join_date between '01-02-2013' and '08-03-2013'
it returns row value 1;
Please suggest ,thanks in advance
The dates are compared alphabetically and not as datetime stamps.
If you can influence how the dates are stored, use a format such as ISO-8601 yyyy-MM-dd or unix epoc timestamps to make the comparisons work.
If you cannot influence the data, you can convert the values in SQL by picking up the components with substr() and concatenating together with ||, like this:
substr(join_date,7,4) || substr(join_date,4,2) || substr(join_date,1,2)

Resources