Classic ASP, DATE difference between IIS6 and IIS8 - ms-access

I am helping to migrate a classic asp website (front end) and ms access database (back end) from a Windows 2003 IIS6 server to a Windows 2012 IIS 8.5 server. I am having a problem with this query in particular;
sqlquery1 = "Select RentalNum, CarID, RentalStatus, StartDate, EndDate from table where CDate(EndDate) <= CDate('"&Date()&"') order by CDate(EndDate) desc"
On the existing system all is ok. On the new system the returned results are not <= "todays date". The results show some dates before today and some after. The database date fields are just "text" (I didn't set it up) and whilst my initial thoughts were to change the schema to proper dates, I would like to understand the problem, particularly as there are many of parts of the system using similar queries using date() and CDate. Are there underlying dates differences between IIS servers? I have looked at browser locality and all is ok there.
Any pointers?

Examine this expression used in your query:
CDate('"&Date()&"')
The Date() function returns the system date as a Date/Time value. But then that expression adds quotes before and after the Date/Time value, which transforms it to a string value. And then CDate() takes that string and transforms it back to a Date/Time value again.
Hopefully that description convinces you those manipulations are at best wasted effort. However if the two servers have different date format settings, the dates resulting from that full CDate() expression could be different.
I'm not positive that is the source of your problem, but you can easily eliminate the possibility. The Access db engine supports the Date() function, so you can use it directly without first transforming it to a string and then back into a Date/Time value.
sqlquery1 = "Select RentalNum, CarID, RentalStatus, StartDate, EndDate " & _
"from [table] where CDate(EndDate) <= Date() order by CDate(EndDate) desc"
If that change does not solve the problem, next examine those EndDate text values and make sure they're interpreted as the date you expect:
SELECT EndDate, CDate(EndDate) AS EndDate_as_date
FROM [table];
You mentioned your "initial thoughts were to change the schema to proper dates". I think that is the best way to go because developement based on dates as Date/Time is saner than with dates as strings.

Related

MS Access Date/Time format issue?

This might be a possible duplicate, but I have a strange issue with MS Access DB.
When I update a Date/Time value for my column req_date, I'm facing a problem.
10-03-2017
is getting updated as below
10-03-2017
where as
10-18-2017
is getting updated as below
18-10-2017
I'm using the following code in c# to Execute the query:
query = "update gb_jobs set req_delivery=myDate"
myCMD = new OleDbCommand(query, con, trans);
int tempCnt = myCMD.ExecuteNonQuery();
where as myDate is already converted to string from date time,
As per the solution by Albert, I concatenated my myDate to #myDate# but it is throwing following error:
query = "update gb_jobs set req_delivery=#myDate#"
Error : Data type mismatch in criteria expression.
You don’t mention where/when/how you are updating that column in question.
As a general rule, if the VBA variable type is an actual DATE type variable then you can assign such values directly to a control or recordset in code.
However if ANY of your code uses a string result, then you MUST format the string as USA format regardless of your computer's regional settings. Your regional settings will thus transform the date display to whatever floats your boat.
So any of your date formats have to be of mm/dd/yyyy. Given your examples, it looks like you are following that format. This suggests that you have your DISPLAY set to DD/MM/yyyy. So in theory what you have given so far is correct behaviour.
What this suggests is that your result of 10-03-2017 ACTUALLY means 03/10/2017. So it is in fact March and not October.
Thus in VBA code to update (or query) some data, you have to go:
dtStart as date
dtEnd as date
If you set the value of above two dates, then to query the data, you MUST go:
strWhere = "InvoiceDate >= #" & format(dtStart,'mm/dd/yyyy') & "#" & _
" and InvoiceDate <= #" & format(dtEnd,"mm/dd/yyyy") & "#"
Docmd.OpenReport "rptInvoice",acViewPreview,,strWhere
So any code that will query, or update values with SQL has to re-format the data to USA format (mm/dd/yyyy). What the control and forms will display is as noted whatever you have in windows regional panel, but ALL internal code must format strings as mm/dd/yyyy.
So it not clear how you are making the change, but from what you have given so far, your DISPLAY of info is dd/mm/yyyy, but you are entering the data as mm/dd/yyyy which is correct. If you are entering this data on a form and not using code, then from what you have given your date format as set by windows is dd/mm/yyyy.

Getting records from MySQL based on a DateTime column ignoring the time portion using JPA along with Joda-Time

How to filter rows from MySQL database ignoring the time portion of a given DateTime field in MySQL using JPA?
For example, the following segment of code counts the number of rows from a database table that lie between the two dates given in a column of type DateTime in MySQL.
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Long>criteriaQuery=criteriaBuilder.createQuery(Long.class);
Root<Discount> root = criteriaQuery.from(entityManager.getMetamodel().entity(Discount.class));
criteriaQuery.select(criteriaBuilder.countDistinct(root));
DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa");
DateTime firstDate = dateTimeFormatter.parseDateTime("01-Oct-2013 11:34:26 AM").withZone(DateTimeZone.UTC);
DateTime secondDate = dateTimeFormatter.parseDateTime("31-Oct-2013 09:22:23 PM").withZone(DateTimeZone.UTC);
criteriaQuery.where(criteriaBuilder.between(root.get(Discount_.discountStartDate), firstDate, secondDate));
Long rowCount = entityManager.createQuery(criteriaQuery).getSingleResult();
The two parameters firstDate and secondDate will be in turn dynamic.
How to rewrite this query so that the comparison does not include the time portion in the SQL query which is to be delegated to MySQL.
The column discount_start_date in the entity Discount is designated as follows.
#Column(name = "discount_start_date")
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime discountStartDate;
Seems like you are working too hard.
(a) Apparently, MySQL offers a DATE() function that extracts the date portion of a date-
time field. (I'm a Postgres guy, and don't know MySQL.) You could pursue an approach using that function call as part of your query. But I'm guessing it would faster performance if you first obtained your start and stop time by calculating with Joda-Time in Java before executing the SQL query, as seen below.
(b) Why not do this with a simple SQL query, a two criteria SELECT?
In pseudo-code:
Find Discount records that go into effect from the moment this month starts up until the moment the next month starts.
Use Java and Joda-Time to give you the start & stop values.
org.joda.time.DateTime startOfThisMonth = new org.joda.time.DateTime().dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
org.joda.time.DateTime startofNextMonth = startOfThisMonth.plusMonths( 1 ).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
Caution: Above code uses default time zone. You should specify a time zone in the constructor.
MySql seems to lack sophisticated time-date handling with time zones etc. So I suppose you would convert those time zoned DateTime objects to UTC.
org.joda.time.DateTime startOfThisMonthInUtc = startOfThisMonth.toDateTime( org.joda.time.DateTimeZone.UTC );
org.joda.time.DateTime startofNextMonthInUtc = startofNextMonth.toDateTime( org.joda.time.DateTimeZone.UTC );
Then do what you do to get date-time values for MySQL.
Then form a query that looks something like this… (Note the use of >= versus < without the Equals sign.)
SELECT title_, amount_, start_date_
FROM discount_
WHERE discount_.start_datetime_ >= startOfThisMonthFromJodaTime
AND discount_.start_datetime_ < startOfNextMonthFromJodaTime
;
When working with date and time, it's generally better to work with the first moment of the day, first moment of the first day of month, etc. rather than try to find the last moment or end time. So my query is based on the idea of find rows whose values go up to, but do not include, the moment after the time frame in which I'm interested.

How to set the year part of a field using a query

Evening,
I have created a query that is suppose to change ONLY the year part of a Date/Time field to 1900 when a person is 89 years or older. The query that follows compiles fine but when run it complains about a Type Conversion failure and removes the entire value from the records affected.
The query:
UPDATE tblTestForDOB
SET tblTestForDOB.[PT_BirthDate] = DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
WHERE Year(tblTestForDOB.[PT_BirthDate]) <= Year(Date())-"89";
According to the MS Help (F1 over the function):
The required date argument is normally a string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range.
Is that not what I'm doing? I also tried placing the " " & before the values inside the DateValue function and that did the same thing
(to ensure that it was a string that was passed)
So how do I go about it? Should I use CDate to convert the value to a Date and then proceed that way? If so what is the correct syntax for this?
Thanks
P.S The field is of Short Date format. Also note that I don't want to take the long way around and use VBA for the whole thing as that would involve opening record sets and so on...
It appears you're trying to give DateValue a string, but that's not what's happening. There may be more going on that I don't understand, so I'll just show you an Immediate window session which may contain something you can build on.
PT_BirthDate = #1923-6-1#
? PT_BirthDate
6/1/1923
? DateDiff("yyyy", PT_BirthDate, Date())
90
' this throws error #13: Type mismatch ...
? DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
' it will work when you give DateValue a string ...
? DateValue("1900-" & Month(PT_BirthDate) & "-" & Day(PT_BirthDate))
6/1/1900
' or consider DateSerial instead ...
? DateSerial(1900, Month(PT_BirthDate), Day(PT_BirthDate))
6/1/1900

How Can I Perform Date Comparisons in Access 2013 Query Criteria?

I have a date field in my table, and I'm writing a query in Access 2013 to select all items where the date is between 7-days-ago and 30-days-in-the-future.
Currently, I've added the following as "criteria" under the date field:
>=Today()-7 And <=Today()+30
But I get the following error when I try to save the query:
I've tried using DateDiff (as I have in other scenarios) but it tells me that I'm not allowed to use that type of expression as criteria.
EDIT: This is an Access 2013 custom web app for SharePoint 2013, and all the available functions and syntaxes appear to be different from those available in a desktop database file.
You might be confusing with the Excel function named TODAY(). In Access it is called Date().
You can also use Between..And.
Between Date()-7 And Date()+30
Added In response to advice about using SharePoint:
I don't use SharePoint, but might guess that you need to specify the field explicitly:
fieldName >= Today()-7 And fieldName <= Today()+30
you might use brackets to make the statement clearer:
(fieldName >= Today()-7) And (fieldName <= Today()+30)

MS Access 2007: date query

I need help with date queries in MS Access 2007.
How do I show all data between date:01/06/2010 time:10:51 and date:13/07/2010 time:22:30?
If you are using the query design window you have a lot more latitude than if you are working in VBA. In the query design window you can enter a date and time on the criteria line in the format for your locale, when viewed in SQL view, you might see:
SELECT tbl.CrDate
FROM tbl
WHERE tbl.CrDate Between #2/5/2006 14:7:0# And #11/18/2006 17:28:15#
However, it is generally best to enter dates in year/month/day or year-month-day format, even though Access may change it to your locale format. In VBA it is a different story, Access needs month,day,year order or year,month,day. Once again, year,month,day is better.
As regards your problem, if you have separated the date and time fields, it would be best to reunite them for the query, you can use + :
DateField + TimeField Between #01/06/2010 10:51# And #13/07/2010 22:30#
I have not used MS Access for years, so this is only from memory: Access uses # instead of ' for date values. And you need to use ISO format:
WHERE datecolumn >= #2010-06-01 10:51# AND datecolumn <= #2010-07-13 22:30#