Crystal Report adding a year or month based on IF condition - vb.net

I am trying to add a year to currentdate in Crystal Reports after a if condition (if a user renews a month it will add a month, a year it will add a year to current date), but it's not working. I created a date field type and in formula editor I put the below formula I tried the below
If {tblcustomers_1.Customertypeofpay}="yearly" then DateAdd("yyyy", 1, CurrentDate)) else
DateAdd("mm", 1, CurrentDate))
I tried so many ways for date add and still not working please help.

Change it to:
If {tblcustomers_1.Customertypeofpay}="yearly" then
DateAdd("yyyy", 1, CurrentDate) else DateAdd("m", 1, CurrentDate)
You had extra ')' and extra 'm'.

Most likely, your typeofpay field is numeric rather than text.

Related

SQL connection within Excel- date range

I need to be able to look at the previous 3 months of data using a SQL statement within Excel.
I have the below code which I've been able to build but it doesn't do what I need 100%.
It gives me the last 3 months and the last date of the 4th month and the first date of the current month.
I could use Excel VBA and delete out data that is the first of current and last of the month 4 months ago.
However, I was wondering if anyone could help correct the below.
I've tried using EOMNTH and this brings an error up and I cant seem to use a DECLARE statement either.
If the below could be changed to only bring back information from the last 3 months (Jan, Feb and March) that would be great.
If someone could explain how I could change the below for only dates within the previous month that would be helpful too.
where T1."Receive_Date" between DATEADD(dd, -DAY(DATEADD(mm, -4, getdate())), DATEADD(mm, -3, getdate()))
and DATEADD(dd, -DAY(getdate()) + 1, getdate())

Query to return all values from next month

I have a dataset of subscriptions in a table that looks something like this:
Table:
UserID
Subscription_type
Subscription_end_date
I'm trying to build a report in SSRS that shows a list of all subscriptions that are going to expire somewhere in the next month. The report gets sent on the first of every month and needs to show all the rows that are going to expire in the next month.
For example: a report sent on 01-07-2021 needs to show all subscriptions that are going to expire during August (so all subscription_end_date with a value from 01-08-2021 to 31-08-2021).
At first I thought using a where statement like the below one would be the answer:
WHERE [Subscription_end_date] = DATEADD(MONTH, 1, GETDATE())
But somehow it doesn't return any values. I would like to know if my solution is in the right direction and that DATEADD combined with the GETDATE should do the trick or that I have to search it in a whole other statement.
You need to just check your target date is within the desired range,
try
where Subscription_end_date >= dateadd(month,1,convert(date,getdate()))
and Subscription_end_date < dateadd(month,2,convert(date,getdate()))
Assuming of course you are running your report on the 1st of each month.
The first date of the next month can be found using:
dateadd(day, 1, eomonth(getdate()))
So, you can use this in a where clause:
where Subscription_end_date >= dateadd(day, 1, eomonth(getdate())) and
dateadd(day, 1, eomonth(getdate())) < dateadd(month, 1, dateadd(day, 1, eomonth(getdate())))

SQL in VBA WHERE Clause

I have been trying to add additional filter criteria to my WHERE clause in sql VBA. The previous select statement works fine, but I can't seem to get the updated WHERE clause to work. Here's what I have:
WHERE tblretirements.Applicationcancelled = 'No'
AND tblretirements.FirstPayDate IS NULL
OR tblretiremetns.FirstPayDate BETWEEN 'now()' & 'Beginning of Prior Fiscal Year'
I am not that familiar with the BETWEEN statement and am positive that I am messing that up. I need to have the code dynamically reference today's date and the beginning of the prior fiscal year, which is 6/1/2017 right now. Can someone please help? Thank you.
The sytax of BETWEEN statement is field_name BETWEEN aaa AND bbb,
NOT field_name BETWEEN aaa & bbb.
modify your code
tblretiremetns.FirstPayDate BETWEEN 'now()' & 'Beginning of Prior Fiscal Year'
to
tblretiremetns.FirstPayDate BETWEEN date() AND dateserial(year(date()) - 1, 6, 1)
Which now() function returns date and time, and date() function returns date only.

Report that updates yearly

I have created a report that is supposed to look at the number of baptisms at our church for the ministry year. The Ministry year runs from Aug 1 - July 31. I currently have the report set to tell me the names of anyone that has a baptism date greater than 8/1/2016. But I would need to change that year each year for it to report properly. so I wanted to use a Case statement to have it update each year, but i am getting an error message with this: (The error is in the where clause, so I didn't include the entire report)
WHERE (P.organization_id = 1) AND
((CandidateProcesses_BaptismDate68.datetime_value) between (
case
When datepart(month, getdate()) < 8 then ('8/1/'+ datepart(year, getdate()))
When datepart(month, getdate()) >7 then ('8/1/'+
datepart((year,getdate())-1))End) and Getdate())
Does anyone see why I am getting an error?
Thanks!
You are getting an error trying to add a string and a number. You could fix that using datename() rather than datepart(). But, I think this is a simpler approach:
WHERE (P.organization_id = 1) AND
year(dateadd(month, -7, CandidateProcesses_BaptismDate68.datetime_value)) = year(dateadd(month, -7, getdate()))
This subtract 7 months to get the "ministry year" and then compares that to the current date minus seven months. That is, it subtracts 7 months and then normalizes on the calendar year.
This is a bit more expensive than your version, because it cannot use an index on CandidateProcesses_BaptismDate68(datetime_value). However, I doubt the database of baptisms is so large that the query will take very long anyway. (If that is an issue, then your version can be made to work with some simple modifications.)

SQL issue: cannot construct data type date some of the arguments have values which are not valid

I am working on an Excel project where I use SQL query to extract some data from SQL Server 2012.
When I select the data without filters, everything works fine.
However, when I use the DATEFORMATPARTS formula below, I get this error:
Cannot construct data type date some of the arguments have values which are not valid
A.[Invoice date] is the correct date format.
WHERE
A.[Customer] NOT IN ('100', '398', 399)
AND A.[Item] LIKE '1%'
AND A.[Invoice date] >= DATEFROMPARTS(Year(DATEADD(yyyy, -1, GETDATE())), Month(DATEADD(yyyy, -1, GETDATE()) >) + 1, 1)
I've tried a lot of different stuff, but without luck.
Any guesses what is wrong in the above.
Thanks a lot in advance!
/ T
Thanks for the input
it was precisely the month: 12 + 1
that was the issue! thanks
You're going to have problems with that month calculation if you get a december and it adds a month to get month 13 of the year, this obviously isn't valid. If you want 11 months ago then do this;
DATEFROMPARTS(Year(DATEADD(YYYY,-1,GETDATE())),Month(DATEADD(mm,-11,GETDATE())),1)
Which (today, December 2016) returns '2015-01-01'
I believe > sign in the expression is a typo
Month(DATEADD(yyyy,-1,GETDATE()))+1
This is resulting 13 which is a invalid month so you are getting that error.
Removing > and +1 from Month part will fix your issue
select DATEFROMPARTS(Year(DATEADD(yyyy,-1,GETDATE())),Month(DATEADD(yyyy,-1,GETDATE())),1)
If you are trying to find last year last month first date then
select datefromparts(year(getdate())-1,12,1)