How to get month within a range vb2010 - vb.net

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.

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)

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

Adding and Updating data by month in Access with Vb.Net

I've been trying to add and update data to a table in MS Access by month. This is so I can calculate any expenditures or sales per month. So if stock for a company were bought in March It would add this to the March expenditure row. And would be kept different from the expenditure in February or April for example.
I am unsure how to do this and how to have my program check the current month and year to see where it should input the data/make a new row for the month.
I know how to write to a database, I'm just not sure how to make my program write to a row in the database that depends on the current month and year
Any help would be greatly appreciated and sorry If this is all long winded or this is an easy fix, its my first post here and I'm a novice when it comes to programming.
In Access, to get the current date you use the date() function. I.E. if you were to do:
SELECT DATE()
the query would return the system date, which, today would be:
26/03/2015
To do math (add or subtract a specified interval) against the date, the syntax, again in Access, is:
DATEADD([Interval],[Number],[Date])
So, if I wanted to add one month to the current date I would write:
DATEADD(m, 1, DATE())
Or conversely, subtract two months from the current date:
DATEADD(m, -2, DATE())

Setting date range in queries

I would like to add a condition in my query to filter records which has a date falling under current month. How can I achieve this? I tried to use clj-time to retrieve the current month and match it along with the DB field. But that didn't give me any luck.
Actually, it's pretty simple to determine the current month using clj-time:
(month (now)) ; number from 1 to 12
But, since you're trying to query a database, you should be interested with the beginning of the current month:
(let [t (now)]
(date-time (year t) (month t)))
This code will return you a valid DateTime object, corresponding to the beginning of the current month. You can use this object t query your SQL database:
(select objects
(where {:created [> start_of_the_current_month]}))