SSRS optional drop down parameter passing dates - sql-server

I currently have a set of drop down parameters that pass dates to a query. There is a display value available to select for each Work Week.
Create Date Parameter:
Display in the drop down: 2016 01 (1/3/2016) P01-16 Q1-16
Value being passed to the SQL is WEEK_BEGIN_DATE: 1/3/2016
New Ship Date Parameter:
Display in the drop down: 2016 01 (12/27/2016) P12-15 Q4-15
Value being passed to the SQL is SHIP_WEEK_BEGIN_DATE: 12/27/2016
I would like to be able to make both of these optional.
I tried the following in the parameter value SQL to get the parameter to allow nulls but I got a data type error. I did start going down the cast as varchar() route but it was getting messy quick.
SELECT 'NULL' AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
Any suggestions are much appreciated. Let me know if I can provide any other helpful info.

this should work, you don't need the single quotes
SELECT NULL AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
to make parameters optional you could in where clause write something like
where (WEEK_BEGIN_DATE = #date or #date is null)

If your parameter of dates was a VARCHAR() you would need to do what Kostya stated. But since it's not, you can click "Allow null value" under Report Parameter Properties (rightclick on your parameter and select properties)

Related

TO_DATE() is returning incorrect format than the format specified in Snowflake

I am trying to convert the date I have got using the GETDATE() function into YY-MON-DD format such as 04 Nov 2022. I have used TO_VARCHAR() to convert into the date returned by the GETDATE() into a string. The output is correct till the TO_VARCHAR() is used i.e. SELECT TO_VARCHAR(GET_DATE, 'DD-MON-YY') returns the desired format.
When I try to wrap it around TO_DATE() function; the date format changes into 2022-11-04.
SELECT TO_DATE(TO_CHAR(GETDATE(), 'DD-MON-YY'), 'DD-MON-YY')
How can I resolve the problem and correct the format!?
A date will display in the default format for your session. If you want to display it in a different format you would need to use the TO_CHAR(date_col, fmt) construct. So your select statement just needs to be:
SELECT TO_CHAR(GETDATE(), 'DD-MON-YY')
wrapping a TO_DATE round it doesn't achieve anything if all you are trying to do is display that column as an output of your SELECT statement
You need set the desired format at session level
alter session
set date_output_format='DD-MON-YYYY';
select getdate()::date --getdate() returns a timestamp not a date
select current_date() --alternatively
As Nick suggested, use the select to format dates instead of tinkering with session parameters

How can I select a date from a table and pass it in as a parameter in SSIS?

I have one single date in one table. I am trying to select this date and pass it into a function that pulls data from another system, based on this date parameter. I think it should be something like this.
Declare #dateCurrent as DateTime
Set #dateCurrent = MyDate
SELECT #dateCurrent = DATEADD(Day,-1,CONVERT(VARCHAR(6),#dateCurrent,112)+'01')
Select top 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', #dateCurrent)
I set 'MyDate' as a Variable. I'm not sure if this is the right way to do it. Or, should I simply select the date from the table, like this.
Select AsOfDate
FROM DATE_RAW_DATA
No matter what I try, I'm getting an error that SSIS can't execute the query because the date parameter isn't coming in. How can I get this setup correctly? Thanks.
First, create an Execute SQL Task that gets the date value from your table and saves it into your variable.
Next, create another Execute SQL Task that is connected after the first task, and then use your variable in the query. Assuming you have the date saved in a variable named MyDate, your query for the second SQL Task would look like this:
SELECT TOP 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', DATEADD(DAY,-1,CONVERT(VARCHAR(6),?,112)+'01'))
The question mark in the query means you're going to be using an input parameter for that value. So once you have the SQLStatement field value set, click on the "Parameter Mapping" option in the left pane of the Execute SQL Task Editor. For "Variable Name", select your variable from the drop down list, and for "Parameter Name" put 0 (which corresponds to the first question mark in the query). Make sure the variable data type and size are set correctly.

SSRS date range parameter

I have a report in SSRS 2016 that contains one SP which has a date field called startdate. I'd like to create a parameter where the user can select between two ranges: startdate >='2017'or startdate < '2017'. This seems like it should be simple, but I can't see to find an easy way to do this.
I've tried to create a parameter in SSRS where I chose "Specify Values" and manually added these values but I get an error that the "expression references the parameter "startdate" which does not exist in the Parameters collection.
I'm sure I can create this by creating a stored procedure with these dates, but that seems like more work than is needed.
Does anyone know of a better way to do this?
If you are looking to have a parameter that has two options, 0 - Before 2017, and 1 - After 2017 then you should just create a Date parameter which has two options, Before 1/1/2017 in the label field with a 0 in the value field and After 1/1/17 in the label field with a 1 in the value field. Then in your report you just have to filter your data based upon the 1 or 0 value.
For example:
DECLARE #DateFilter INT = 1
IF ( #DateFilter = 0 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter < '1/1/17'
END
IF ( #DateFilter = 1 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter >='1/1/17'
END
I don't know why you would filter your data this way. I recommend to use Larnu's suggestion that you have two parameters, a Start Date and an End Date. Otherwise this could return a lot of rows for the second option as time goes on.
Add a couple of parameters in SSRS say, FromDate and ToDate and make its data type as Date/Time.
You can specify the default values for these parameters using DateAdd functions and samples give below:
Today:
=Today()
last week from today:
=DateAdd(DateInterval.Day, -7,Today())
You can add/reduce year, quarter month etc. in a similar way as shown below. Just change the number to the required length
=DateAdd(DateInterval.Year,-1,Today())
=DateAdd(DateInterval.Quarter,-1,Today())
=DateAdd(DateInterval.Month,-1,Today())
=DateAdd(DateInterval.DayOfYear,-1,Today())
=DateAdd(DateInterval.WeekOfYear,-1,Today())
=DateAdd(DateInterval.WeekDay,-1,Today())
=DateAdd(DateInterval.Hour,-1,Today())
=DateAdd(DateInterval.Minute,-1,Today())
=DateAdd(DateInterval.Second,-1,Today())

Microsoft SQL Server - Open Query where > date

I am trying to run a select using openquery with filtering result by date, but I have problem with using the date after where clause.
Ideally I would like to be able to pass a variable
set #d = dateadd(day, -30, getdate())
but for the sake of example I will try to use specified date :
Example:
select *
from OPENQUERY([Linked_Server], 'select id, name from Users where LastModifiedDate > ''2017-01-01''')
This returns an error:
INVALID_FIELD:
select id, name from Users where LastModifiedDate > '2017-01-01'
value of filter criterion for field 'LastModifiedDate' must be of type dateTime and should not be enclosed in quotes".
It works ok if I use for example istrue = true, but comparing dates seems to be the problem.
Can someone please advise me on this ?
It looks like you're querying a linked server that is not standard SQL Server but is instead Salesforce which uses SOQL which has a specific format for date and datetime literals. The correct format for date filters in Salesforce is:
WHERE LastModifiedDate > 2017-01-01T00:00:00Z
So your full SQL should be:
SELECT *
FROM OPENQUERY(
[Linked_Server],
'SELECT id, name FROM Users WHERE LastModifiedDate > 2017-01-01T00:00:00Z')
WE use a lot of Open queries here and we've stumbled into that kind of scenario. What we have done in the past is this:
CONVERT(VARCHAR(11),#d,101)
OR
CONVERT(VARCHAR(25),#d,126)
This already converts the date into the format DavidG posted there.
Also, to double check if the query is coming out correctly, we assign the query text into a variable and then we use the print to show the variable, that print which is just simple text that will show on your Message tab alongside your resultset tab. As long as the Where clause shows with only two single quotations, the query you have should work, just in case of doubt, copy the message and run it separately by replacing all doubled single quotations into one single quotation only.
What I had in my message tab on WHERE clause was something like that:
WHERE thisdate BETWEEN ''02/27/2017'' AND ''2017-02-27T23:59:59.990''

How to set date format for the result of a SELECT statement in SQL Server

I know how to use CONVERT function in SELECT statement to change the format of the Date column:
SELECT
StationID
, CONVERT(varchar, [Date], 101) as Date
, Value
FROM my_table
But I was wondering if I can set the date format in general before running the SELECT statement, when I don't know the name of the date column in the following SELECT statement:
SELECT * from FROM my_table
Is any SET statement or other T-SQL that I can run before my SELECT statement so that I can change the Date format temporarily?
Thank you
No.
In particular, any date columns which you select are not actually formatted at all, but are instead returned down the wire as an actual piece of date data in a binary "format" which is used for dates. If you are seeing them formatted, it's because your client (either management studio or some other tool) is converting them to strings to display.
When you use SELECT *, there is obviously no way to tell SQL Server to do any conversions on any particular columns, so the data is going to be returned in whatever the data types of the underlying query returns. So regardless of whether your data types are really date or not, no manipulation is going to happen at that point anyway.
I'm pretty sure there's no way to do what you're asking. However, there are ways to format the date string when you output it using your programming language.

Resources