access convert string like "FEB 2017" to date - vba

report out of account system comes out with month like "FEB 2017". I need to convert that string to a date that is the end of the month like 02/28/2017. Any ideas?

SELECT LastDayInMonth(DateValue(Mid("Feb 2017", 1, 3) & " 1, " &
Mid("Feb 2017", 5, 4))) AS LastDayInMonth
FROM yourTable
Explanation:
The concatenated term inside the call to DateValue() will be Feb 1, 2017, and will evaluate to the same date, at least for the sample data I used. In general, it will be the first day of the month for the data you showed us. Then, we use LastDayInMonth() to shift that date to the last day of the same month.

You can also use native functions, adding one month, subtracting one day:
MonthYear = "FEB 2017"
Ultimo = DateAdd("d", -1, DateAdd("m", 1, CDate("1 " & MonthYear)))
Ultimo -> 2017-02-28

Related

Visual basic code to show dynamic date range change in SSRS report

Hi I created a SSRS placeholder [Date] in one SSRS report.
Date Range: [Date]
And the expression of the placeholder is:
=Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")
However, I set the start_date and the end_date default to Null. When I preview the report, this placeholder would show something like this:
Date Range: -
If I change the start date to 3/1/2020 and end date to 3/31/2020, then it will show:
Date Range: Sunday, March 1, 2020 - Tuesday, March 31, 2020
How can I change my expression to make the default Date Range to be from the oldest date to the latest date if any or both of them is/are Null? Here is my Visual Basic code, I tried my best but I did not have a lot of programming experience, so it may not be true:
=If start date is null
then Microsoft.VisualBasic.Strings.format(min(Fields!arrival_date.Value), "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")
if end date is null
then Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(max(Fields!arrival_date.Value), "Long Date")
if start date and end date is null
then Microsoft.VisualBasic.Strings.format(min(Fields!arrival_date.Value), "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(max(Fields!arrival_date.Value), "Long Date")
else Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")
I'm not sure you need any VBA to accomplish this. The way I would do this is as follows.
Createa a report.
Add 2 parameters StartDate and EndDate
I would then create a dataset something like (taken from the sam,[le adventureworks database)
SELECT CustomerID, OrderDate, SubTotal
FROM Sales.SalesOrderHeader
WHERE (SubTotal < 100) -- just to reduce the sample data
and
(
(OrderDate >= #StartDate OR #StartDate IS NULL)
AND
(OrderDate <= #EndDate OR #EndDate IS NULL)
)
This will use the dates provided or is either are NULL it will resort us using the first or last record found.
Then for you place holders the [Start] placeholder would be something like.
=IIF(
Parameters!StartDate.Value=Nothing
,First(Fields!OrderDate.Value, "DataSet1")
, Parameters!StartDate.Value
)
and in a similar fashion of the [End] placeholder
=IIF(
Parameters!EndDate.Value=Nothing
,Last(Fields!OrderDate.Value, "DataSet1")
, Parameters!EndDate.Value
)
This will either show the selected start date parameter or the first record found if not parameter was specified and the same for the end date except if will show the last record found.
Finally I formatted the placeholders to show as long dates.
The results look like this with none, one and both parameters selected.
(actual data continues to be displayed up to 30th June 2014)
We how the first and last order date for the entire dataset as not parameters were specified.
Here the first order date after the 20th May 2011 is 1st July but the parameter date is shown as that is what the user selected
Finally the parameter dates are shown in both placeholders even though the data does not extend that far as that is what the user selected.

How to extract records from SQL table falling within certain date range? [duplicate]

I have a SQL statement to display data between two dates. I almost got it but there's a problem.
If I input March 1,2012 to March 7, 2012.. it should show data with dates between the two.. but it also show all of the dates under March 2012.. but whenever I input March 10, 2012 to March 30, 2012 the SQL works perfectly.. any help will be appreciated. thanks
SELECT
agentname, noofcalls, qualified, booking, resched,
actualbooking, sales, remarks,
concat(month,' ',day,',',year) as 'date'
FROM
tblagents
WHERE
(month between '" & cbosmonth.Text & "' AND '" & cboemonth.Text & "')
AND (day between '" & cbosday.Text & "' AND '" & cboeday.Text & "')
AND (year between '" & cbosyear.Text & "' AND '" & cboeyear.Text & "')"
you are doing string comparisons in each of your 'between'. Every number starting with a 1, a 2 or a 3, regardless of what follows it, i.e. 21, or 26, or 31, they are all lower than 7 if you look at them as strings. 1 to 30 works because you're only leaving 31 behind, and 30 < 31 as a String as well.
Do the concatenation first and then the between:
WHERE concat(month,' ',day,',',year)
BETWEEN concat(cbosmonth.Text,' ', cbosday.Text,' ',cbosyear.Text)
AND concat(cboemonth.Text,' ', cboeday.Text,' ',cboeyear.Text)
(check out for correct syntax, I'm just copy pasting from your question, not tried it)
BTW, unless you have a reason to, you probably should be storing the entire date in a single column with the right data time (datetime, timestamp, ...) and not three separated columns.
That is the incorrect way to store a date in database.
Change the date column to datatype date.
You could then do:
SELECT *
FROM table
WHERE bookingDate between to_date ('2012/03/01', 'yyyy/mm/dd')
AND to_date ('2012/03/07', 'yyyy/mm/dd');
This approach is wrong.
In order for a date to be between two interval dates it does not have to have a day number between the two dates, e.g. (pseudocode)
date = May-25-2012; startDate = March-15-2012, endDate = June-01-2012
date is clearly between startDate and endDate, yet
day(date) is 25, which is not between day(startDate) = 15 and day(endDate) = 1
more, as 15 is larger than 1, there are no numbers between them, so that condition will always be false
Similar example can be made for the month part of the date (e.g. date = May-25-2012; startDate = September-15-2010, endDate = Match-01-2015)
You need to take the values for day, month, year, and construct a Date either in the application or on the server and use that to compare the values against.
To make a date from text fields in VB
Dim startDate = new DateTime(
Convert.ToInt32(cbosyear.Text),
Convert.ToInt32(cbosmonth.Text),
Convert.ToInt32(cbosday.Text))
Note that this will fail if the user enters, e.g. "some text" for the year value. You'll need to add some data validations to achieve that.
To make a datetime from parts in SQL Server, take a look here, there are quite a few techniques explained.
Also, you should always avoid just pasting values into the sql string, that's asking for sql injection problems. You should do something like this:
Dim command = new SqlCommand()
command.CommandText = "SELECT .... FROM tblagents where DATEFROMPARTS(year, month, day) between #startDate AND #endDate"
command.Parameters.AddWithValue("#startDate", startDate)
command.Parameters.AddWithValue("#endDate", endDate)

SQL: How to get the date yyyy/mm/dd based on the year and day number?

I have the following string 2015089 or 2016075, for example.
I need to get the result in yyyy/mm/dd format based on the given input.
So, based on 2015089, I get, 2015/mm/dd. dd is a 89th day of 2015 and mm is a month that has 89th day.
How can I do something like that?
I think the simplest way is to convert to a date using dateadd():
select dateadd(day, right(str, 3) - 1, datefromparts(left(str, 4) + 0, 1, 1) )
That is, add one less than the number of days to the beginning of the year. This assumes that Jan 1 is represented as "1" and not "0".
You can then format the date however you like.
In pre-SQL Server 2012, you can do:
select dateadd(day, right(str, 3) - 1, cast(left(str, 4) + '0101' as date))

VBA Code for name of previous month

I have added text in to my email that goes like:
"please provide numbers for MMMM month end" - where MMMM is the name of the previous month.
So it it's April today, MMMM will show March.
I have the following code:
Dim newDate: newDate = DateAdd("M", -1, Now)
But the result comes out to be 27/03/2017 16:37:58
I want it to show March.
Any suggestions?
Format the return as "MMMM":
Dim newDate: newDate = Format(DateAdd("M", -1, Now), "MMMM")
If set to True, the month name is abbreviated e.g. Apr
newDate = MonthName(Month(DateAdd("m", -1, Date)), False)
This will ensure the month is in English irrespective of the regional settings on the computer. This will be helpful for those who are releasing an Excel VBA tool worldwide.
previousmonth = WorksheetFunction.Text(DateAdd("m", -1, Date), "[$-409]mmmm")
For abbreviated months eg: Apr use
previousmonth = WorksheetFunction.Text(DateAdd("m", -1, Date), "[$-409]mmm")

Convert Text to Date

I have a text field in Access called "TempD"
which contains data like below
Mon, Oct 6, 2014
Mon, Nov 10, 2014
I need to convert this to proper date format using SQL and put it in Field "Emaildate"
I am using the sql query below:
UPDATE MissingT
SET MissingT.emaildate = format(cdate(Right([tempd],4) & "/" & Mid([tempd],6,3) & "/" & Mid([tempd],9,2)));
But the issue is that the TempD field varies in length if the day is single/double. When the day is in 2 digits
(Mon, Nov 10, 2014)
it is converting this text to 2014-11-01 instead of 2014-11-10
Any idea how can this be resolved?
Discard the abbreviated day name, comma, and space from the beginning of your date string ...
? Mid("Mon, Oct 6, 2014", 5)
Oct 6, 2014
The resulting substring can be converted directly to a Date/Time value using CDate() ...
? CDate(Mid("Mon, Oct 6, 2014", 5))
10/6/2014
Then if you want that Date/Time value as a string in yyyy/mm/dd format ...
? Format(CDate(Mid("Mon, Oct 6, 2014", 5)), "yyyy/mm/dd")
2014/10/06
If that is what you're after, you can do it in an UPDATE query like this ...
UPDATE MissingT
SET emaildate = Format(CDate(Mid(tempd, 5)), "yyyy/mm/dd");
It makes no sense to apply Format on a date value passed to a Date field.
This is how to solve it:
UPDATE
MissingT
SET
emaildate = DateValue(Mid([tempd], 5))