SSRS Generic Drop Down Month Parameter - sql

I am using Visual Studio 2012 and SQL Server 2012. I'm trying to create a simple drop down parameter to show generic month/year over a given timeframe, lets say last 5 or 6 years (for example... Sept 2016, Oct 2016, etc).
Edit: My goal with this is that I have a StartDate field for a record and all records that fall within a certain Month, I want to capture with the parameter.
I have already been using a parameter for Start Date and End Date using Date/Time field when creating my parameter to get a range. I no longer wish to use that because my end user wants it simply by month/year.
This is the code I have which shows only the Month of the Start Date right now. How do I amend this code to show Month and Year?
SELECT DISTINCT DATENAME(month,A.[STRT_DTTM]) AS [Month]
FROM Work as A
WHERE A.[STRT_DTTM] IS NOT NULL
ORDER BY DATENAME(month,A.[STRT_DTTM])
Additionally once code is amended, how would I go about this to sort month by normal calendar flow (aka Jan, Feb, Mar, Apr...?)

Try SSRS, which you probably have -
Then have a look at this - Basically you choose the 'Date' type for the drop down parameter: https://technet.microsoft.com/en-us/library/aa337401(v=sql.105).aspx

SELECT DISTINCT DATENAME(month,A.[STRT_DTTM]) AS [Month]
FROM Work as A
WHERE A.[STRT_DTTM] IS NOT NULL
ORDER BY DATENAME(month,A.[STRT_DTTM])

Related

Querying data from quite old IBM iseries AS400 DB2 (date format issue)

I am quite a newbie in querying data from databases and now I am currently having an issue with date format in very old (I don't know exact version) IBM iSeries AS400 DB2 database. My problem is that the date is stored in this DB in three separate columns as whole number (column day + column month + column year) and I need to connect to this DB via ODBC in Excel and filter just a few rows according to desired date span (e.g. from 1st December 2019 until 31st December 2019). In this case I don't want to use PowerQuery to do all the modifications, because the complete table has millions of rows. I want to specify the filter criteria within SQL string so the PowerQuery doesn't have to load all the rows...
My approach was following:
I've created 6 parameter cells in Excel sheet where I simply defined Date From (e.g. cell 1 = '01', cell 2 = '12' and cell 3 = '2019') and Date To (same logic for parameter cells 4, 5 and 6). Then I mentioned these parameter cells in SQL string where I defined:
(Day >= Parameter cell 1, Month >= Parameter cell 2, Year >= Parameter cell 3)
and
(Day <= Parameter cell 4 etc.)
This worked quite good for me, but only when I liked to export just a few hundreds of lines within the same year. But now I am facing to an issue when I like to export data from 1st December 2019 to 31st January 2020. In this case my "logic" doesn't work, because Month From is '12' and Month To is '01'.
I've tried another approach with concat SQL function to create text column like '2019-12-01' and then convert this column to datetime format (with cast to varchar8 first), but it seems that this approach doesn't work for me, because everytime I get an error which says: "Global variable DATETIME not found".
Before I post you some of my code, could I ask you for an advise, if you can think of a better solution or approach for my issue?
Many thanks and have a great day :-)
A simple solution would be
select * from table where year * 100 + month between 201912 and 202001

add month to curent date in aqua data studio

I am trying to develop a report through aqua data studio by using SQL.I am trying to extract report of last month like counting the total number of new users in the last month. Date_Creation is the column when a user is registered in the system. I have tried DATEADD("MONTH",-1,Current_Date) but getting error "Month is invalid in context" .Any Solution/Suggestions?
DB2 doesn't use DATEADD for date arithmetic, you just, err easier to show.
SELECT *
FROM TABLE
WHERE DATE_CREATION > CURRENT DATE - 1 MONTH;
If you want to add a MONTH, why are you trying to "add" a "date" (which is what I assume DATEADD might mean)? What does it mean to add two DATEs together? I.e. what is the result of '2019-01-01' + '2019-01-01' ?!
Anyway, I digress. You can use ADD_MONTHS if you wish https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.ref.doc/doc/r0053628.html
or, use the - 1 MONTH solution, which is just as good (if not better)

Sort by month value in ssrs

I have a pretty awkward setup in SSRS with custom grouping options (i.e. by office, seller, buyer etc.). In example, user can group items by year and then by month. The outcome of the report is then:
Gr1. | Gr2.
2015 | April
2015 | August
2015 | February
2015 | January
and so on...
So both columns are alphabetically ordered, which works excellent for all custom grouping options but months - which should have their own logic for sorting. How could I implement that?
You should try to return all information form the database in its native type. Instead of returning the month names as ‘January’, ‘February’ etc, it would be better to return 20150101, 20150201 for example (or whatever the default date format is for your environment)
You can then alter the format of the type in the report. For example, set up the cell to return
=MonthName(Month(Fields!myDate.Value),False)
To return the name of the Month for any date. SSRS will then know when ordering how to put the dates in the correct order.
ALTERNATIVE
Assuming you do not want to edit the way the data is returned, you can use a Select statement in the code behind your report to manually provide an order for the months.
Supposing the Dataset
Month Val
---------- ---
January 1
February 2
March 3
When sorted by Month returns the following
Instead, insert the following code to your report (right click the report area, select Report Properties, then Code)
public function MonthNumber(MonthName AS String) AS Integer
Dim MonthNum AS Integer = 0
Select Case MonthName
Case "January"
MonthNum = 1
Case "February"
MonthNum = 2
Case "March"
MonthNum = 3
End Select
return MonthNum
end function
Then set the Group Properties to be Sorted on
=Code.MonthNumber(Fields!Month.Value)
Gives the following result
This is because when ordering the set, instead of just looking at the name of the report, it passes the name through the Code, which is effectively telling the report which number to assign to each month. It is then ordered on this value instead of the month's name.
Hopefully this is useful to you. If you have any further question please let me know.

How to get month within a range vb2010

I just don't know how to go about this.
I designed a program that uses MS Access as its database. I have a field for month and year (the field data type is text) where user can register details. The program will register the month and year the user have chosen e.g month= September, year=2011.
My problem now is how to chose a range of data to view by the user using the month and year as a criteria e.g the User may want to view data range from (September 2011 to July 2013).
I couldn't figure out even how to try. Help will be highly appreciated.
Perhaps you could change your application logic to store the month and year as their respective numbers rather than text and change the field data types to numeric.
You could then construct a DateTime object from them, for example September would be 9 and you could use code like the following:
var startDate = new DateTime(year, month, 1); // get year and month as integers from database, uses the first as the date
var endDate = new DateTime(year, month, 10); // change the date but keeps the month and year the same
var endDate2 = startDate.AddMonths(1); // adds 1 month to the date
Alternatively, you could try using a calendar control to allow the user to select two dates instead of building it from a number of fields. Depending on what you are using this could be achieved a number of ways, for example in ASP.Net or WPF you could use two calendar controls and just use their SelectedDate properties as your range.
A range is from a startpoint until an end point. For the startpoint you can add automatically the first of Month. For the endpoint is it more complicated because there is no fix endpoint. What you can do is following:
Write an array that contains for each month the days (e.g. 30 or 31). Except for Febrauary there is a fix pattern.
for Febrauary use the selected year to check is the year a leap year or not. If not add 28, else add 29.
After that create the date string for your SQL:
Startdate 1.9.2011. Do for the entdate the same.
After that, I think you can use the keyword between in your SQL query.
You can assume that everything is entered on the first day of each month. I would pull the information using a query to the database.
select * from [tablename] where DateSerial([colYear], [colMonth], 1) between DateSerial([fromYear], [fromMonth], 1) and DateSerial([toYear], [toMonth], 1)
In this question are some ways to do this:
First. Filter the dates in a range assuming that you use a date like '07-12-2012'
i.e. September 2011 to July 2013
Where DateColumn > '09-01-2011' and DateColumn < '07-31-2013'
OR
Specify a Date and a Year
Where month(DateColumn)='1' and year(DateColumn)='2016'
Note:
There are many ways to do this.
You can Manipulate your statement depending on your desired output.

How to create month year drop down parameter in SSRS 2005?

In SSRS 2005, I need to provide a drop down to the user with month year labels for the last two months.
For e.g. Jan 2010, Feb 2010, ... , Jan 2011, .... Dec 2011
I need this dynamically for the last two years (say).
Additionally, as user select one of the label, I need to pass the last date of the selected month-year combination to the query parameter.
Please help!
Sijoy
Assuming you are reporting from a SQL datasource, create a new dataset with a query to return the last date of each month within the required range (ie. the last two months) and the month-year description of each month - the details of the query will vary depending on the SQL database (MySQL, SQLServer, Oracle, etc). Populate the parameter from this new dataset, and set the value to be the date field and the label to be the description field.