Microsoft Access date comparison in query not working - sql

I added the following field in a query:
IIf(Date()<[NextQDue],"Less","Greater")
The field NextQDue was created in a different query and formatted as Short Date. The value in NextQDue is 12/20/2018. Today's date is 1/1/2019. The query spits out "Less" when it should spit out "Greater." If I change the statement to Date()>[NextQDue] it spits out "Greater" when it should say "Less." I cannot figure this out. I believe I've formatted dates correctly, but nothing is working.

Always handle dates as Date, not text, not numbers, no exceptions.
So, adjust your query and change NextQDue to return a true date value.
If you need to display the value of NextQDue somewhere else, apply the format to the control displaying it.

Related

In Access, How to pick a date range instead of typing two date parameter?

In the Access, i tried to run the query that showing the payroll data between two date. Currently, I need to type two date at the input box. However, I want to use date picker to pick two date to generate the same result. Anyone tell me how to do it?

Need Date Calculation MS Access

I need a flexible table that displays dates. By flexible, I mean, if I put in June 15, 1976, it displays as such. But if I put 20, Access calculates that 20 in the same field as today's date - 20 years.
I set the formatting of the date/time field in table: tbl_ageLimit to #, so it displays a serial date rather than a user readable date. This table has one field: ageLimit.
I am trying to develop a query to recognize if the date is not relevant in its current state and then convert it to something relevant and put it in another table that will constantly update.
Right now, I'm just trying to get formula to work on recognizing and converting the date. This is the formula that works splendidly in excel, but doesn't seem to be working in MS Access:
IIF([ageLimit]<=100,Date()-365*[ageLimit],[ageLimit])
It recognizes if the field has a number in it that is less than 100. But it's not doing the math and displaying a new record in the new table. Below is the sql:
SELECT tbl_ageLimit.ageLimit INTO tbl_allAges
FROM tbl_ageLimit
WHERE (((tbl_ageLimit.ageLimit)=IIf([ageLimit]<=100,Date()-365*[ageLimit],[ageLimit])));
Can someone kindly point out to me what I'm doing wrong? Thank you.
As requested, here are snapshots of the problem:
The table that is created is not showing the fifth record. Stumped.
Consider removing the WHERE clause as you do not need to filter records. Since you intend to evaluate the IIF() expression, place in the SELECT clause:
SELECT IIf([ageLimit]<=100, Date()-365*[ageLimit], [ageLimit]) As [age_Limit]
INTO tbl_allAges
FROM tbl_ageLimit

Date in a short text data type field... Select query trouble

In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'

How to convert a text field to a date/time field in Access 2010?

I am importing an excel file into Access 2010 and the date field(CALLDATE) comes in as text(YYYYMMDD). I would like to use an update query to update a new field "dateofcall" but using a date/time format. I tried to use:
UPDATE tbl_Import SET tbl_Import.dateofcall = CDate([tbl_Import].[CALLDATE]);
I thought it was going to be that simple but it shows up blank with a date format. I also tried to use DateSerriel() but still was getting errors. Any suggestions?
You can use left, right and mid string functions to construct a date from the various parts of the string.
For example:
DateSerial(Left(MyTextDate,4),Mid(MyTextDate,5,2),Right(MytextDate,2))
You can use the above in an Update query to update a date type coulmn 9field) to a the date from the text column.
My date came in as text looking like this:"2013-03-23 00:00:00.0"
I take the left 10 characters only, "2013-03-23", this makes it so Access can recognize it is a date field and then I just switch around the format.
Format(Left([WEEKEND],10),"m/d/yyyy")

sql passing a date to a datetime

I have a few user controls I made in wpf that are driven by two datepickers. One picker controls the begin of the date range and the other one controls the end of the date range.
I had issues with the datepicker.selecteddate property giving me the time along with the date and then my sql results were not all there because the passed in time value filtered out a lot of my results. I ended up finding that if I formatted that time to midnight then my results were all there, but every time I picked a new selection in the datepicker it would reset the formatted time.
After some testing I found that I can just pass the value of the datepicker text property as the parameter. This property's value is a string type and is set to the date value of the current selected date (ex: 3/14/2012 5:00:32 AM is selected date then '3/14/2012' is the text value).
So far, surprisingly, this seems to return all my results I wanted.
I was wondering about why this actually works (is it b/c the 'mdy' literal format is supported and the default?), and if there is any negative drawbacks to doing what i'm doing? I know a lot of times just because something works doesn't mean you should use it in production. I share the tables with others or I would just convert the datetime fields and parameters to date and be done.
I hope my question makes sense. Sometimes they don't. If there are questions leave comments and i'll chime in.
As far as negative drawbacks, I cannot see any - this is how we handle it throughout many apps/DBs. However, the way I typically handle it is if the record being added doesn't need the time (i.e. BusinessDate, LoadDate) then I add the record with the only the Date and no time - so the value is always in the format 3/14/2012 12:00:00 AM which I think makes querying significantly easier since you don't have to deal with the time
Then in my UI (winforms) when performing a search on the date with a datetimepicker I use the datetimepicker.Value.Date which gives the date in a similar format (3/14/2012 12:00:00 AM).
If you have a field in a table that needs a datetime, then use it and you can search the date by either formatting the date in the table or using date > yourdate AND date <= yourdate which would include the date you are searching.