add month to curent date in aqua data studio - sql

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)

Related

Quicksight parse date into 3 days

I know Quicksight could help parse the time into DAY, WEEK, MONTH and YEAR in Field Wells, but how could I customize it into 3 days(72 hrs)? Is there any windowsFunction I could use?
You can introduce a calculated field with formula looking something like that:
addDateTime((extract("DD", {create_time})/3)*3, "DD", truncDate("DD", {create_time}))
And use it for grouping. Depends on your desired time interval it possibly will not do exactly what you need - cause it will group days in month by three, but you can use dateDiff and either some fixed date as starting point. For example:
addDateTime((dateDiff(parseDate("2020-01-01"), truncDate("DD", {create_time}))/3)*3, "DD", parseDate("2020-01-01"))

Calendar control date not known (ASP.Net)

Sometimes we have dates that are unknown, eg: We won't know the date but just the month and year.
eg: --/Dec/2015, the date is not known? How do we accept these kind of values?
Any ideas on can this be stored on SQL Server database?
I would store it as a date in the database. Because with a date you have benefits like:
Calculation function like DATEADD and DATEDIFF.
You will be able to sort on one column
Index will be one column
When you need to get the year and the month. You just use MONTH and YEAR

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

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

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.