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

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

Related

SQL Server : get data where date + last 2 financial years

I wish to find data from where current date and also include data from the last 2 financial years
Basically I want data going back from 'today' to 01/04/2019. I want to add this line of data to dynamically happen, not just a one off for the future
Thus if the date today was 05/06/2022, I would want the date going from that date, backwards until what would be 01/04/2020
The column name is Date - please help
I am quite sure that it should be possible to do it in some simpler way, but you can use something like below:
declare #b date
set #b = datefromparts(year(getdate()),4,1) --first daty of fiscal year in current calendar year
select case when #b<getdate() then dateadd(year,-2,#b) else dateadd(year,-3,#b) end

Apache Lucene ignores months and days in date range search expression

I try to query Apache Lucene index which is built using a database table that contains a date column and my query refers to this very column. In Luke the search expression I use is as follows:
column_name:[yyyy-MM-dd TO yyyy-MM-dd]
The results returned are records which do not have dates (in the queried column) with the year in the start value or older and the ones that do have year after the year in the start value up to the year in the end value. So if I write column_name:[2011-05-22 TO 2015-09-03] - I will get records with the dates in the column with years 2012, 2013, 2014, 2015.
However, the results will not be precise according to the search expression - the month and days value will be ignored. No matter what month and day I will set up - the search will result in records being returned with dates across each of the searched year (from 01.01 to 31.12).
The issue occurs if I use another date format like:
column_name:[yyyyMMdd TO yyyyMMdd]
I am looking to find what might be causing this? Is this a matter of date formatting while indexing? Or is this a matter of date format in the search expression?
I should add for more clarity that the search expression:
column_name:"yyyy-MM-dd"
will return records as exepcted - so with the date included in the expression.
Analysis is the issue.
When you index, your dates are getting split into three terms: "2011", "05", and "22". Any one of the terms can be matched against a range query, but not all three. So for [2011-05-22 TO 2015-09-03], the term 2011 does not fall lexicographically in that range, while 2012, 2013, 2014 and 2015 all do. For another example, if you searched for [1999-01-01 TO 2000-01-01], you would also find all dates on the twentieth of a month, because 20 falls within that lexicographic range.
Since this field is just a date, you should just avoid tokenizing it. Use StringField instead of a TextField, and re-index.

SSRS Generic Drop Down Month Parameter

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])

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.