Check whether a form date field is equal to 1899-12-30 00:00:00.000 - sql

I know this should be simple but after hours of googling I am still failing...
I have a vb.net form application. Basically I have an SQL database with an Employee table with an EmpFinish column in datetime, null format (Employee Finish Date). The data is imported from a linked SQL DB (populated from an external app). Any "blank" dates show as 1899-12-30 00:00:00.000 in SQL.
The Employee table data is shown on a datagridview form, a row is selected, then I want to perform a check to test whether that rows EmpFinish date from the form is earlier or equal to today and is not equal to it's "blank" value i.e. has the employee left employment.
Code excerpt:
Dim currentDateTime = DateTime.Now
Dim selectedEmpFinishDate As DateTime =
EmployeesDataGridView.SelectedRows(0).Cells(5).Value
If selectedEmpFinishDate.CompareTo(currentDateTime) <= 0 Then
' code to do
End If
I didn't start out using the CompareTo() above util I started Googling this issue and it seems this is the best way to compare dates. Yes???
This test works for actual dates e.g. yesterday but also catches all 1899 dates, I have tried lots of tests (using 1 If statement of separate ones) to check for the 1899 blank dates but cannot get anything to work. If I debug the code at the test line, selectedEmpFinishDate supposedly equals #12/30/1899#. If I show the field value onscreen via
MsgBox("selectedFinDate = " & selectedEmpFinishDate) it reports as 00:00:00
I know this must be possible but cannot figure it out.
Could someone please offer a solution as I am running out of hair to pull out.

I have followed djv's advice (I had the same thought last night) and done an override to set these date values to NULL. I was then able to use IsDBNull() successfully.
To clarify where these dates came from... Data is imported into this database from another SQL DB created by a huge business software application. Not sure if they want the dates stored this way or whether the import sets them to be this "blank" default 1899 date.
They actually showed on all forms as 1899 etc so resetting them to NULL is the best option all around.
To anyone else who has these 1899 dates I would advise to update them to NULL.
Thank you to everyone who responded especially djv.

Related

IF statement in VBA that proceeded incorrectly

I have an Access VBA Function that sends a number of emails containing updated Excel spreadsheets, but only if the underlying data has been updated recently. This piece has been working just fine until last night when if seems that the IF statement incorrectly compared the system date to the data date and sent emails using the older data instead of sending a warning email to the person responsible for correcting the issue of having old data.
Access 2016 on a VM running Windows 10. The current data date is contained in a table and that date is correct. The system date is correct. Everything worked as it should except for comparing the dates. There were no failures or error codes generated.
Boiled down to the trouble spot the code reads ...
' check to see if data is out of date
If ERDBDate < Now() - 2 Then
' send notification of old data
Else
' send updated spreadsheets
End If
Although my experience in coding is not that great, I have never seen an IF statement improperly evaluate like this. All I can assume is either I am using a "shaky" method of comparing the dates or there was some kind of hickup on the server side that caused an incorrect system date to be returned (does this even seem possible?). I would rather it be me then not being able to rely on something as "simple" as the system date.
I have learned a tremendous amount from the answers and help posted on this site. Thanks for all your efforts!
DateDiff is the proven method for comparing date and time. Or use Date() to have no time (with DateDiff however, that doesn't matter):
' check to see if data is out of date
If DateDiff("d", ERDBDate, Date) >= 2 Then
' send notification of old data
Else
' send updated spreadsheets
End If
Try this statement:
If ERDBDate < DateAdd("d", -2, Now()) then

Excel Forumla/VBA to skip blank columns for calculation

I have a particular data set that consists of information for projects our company works on. Each project can go through five different statuses and we have a column that records each date the project is put into that particular status.
Now for the Excel part. We are trying to calculate the days in each status and find the total project time. The total is easy to do, because I can use network days between the project submitted date and the project go live date. The statuses, however, are giving me issues because sometimes a project will skip a status, leaving the date field empty. So what happens is a project goes from status B to status D, the formula for days in status "B" look for a date in column "C" to use as the second date in a NETWORKDAYS formula. When it is empty, the IF argument tells the formula to use TODAY() as the second date. What I need it to do is the search the columns to the right (within the given range of A:E for that row) and use that date if it exists. If not, then it can default to TODAY() because this would be an "active" project that has not moved on.
=IF(IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",IF([#INTAKEDATE]="","",IF([#SCOPEATE]="",NETWORKDAYS([#INTAKEDATE],TODAY()),(NETWORKDAYS([#STATUSADATE],[#SCOPEATE])))))<0,"",(IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",IF([#INTAKEDATE]="","",IF([#SCOPEATE]="",NETWORKDAYS([#INTAKEDATE],TODAY()),(NETWORKDAYS([#STATUSADATE],[#SCOPEATE])))))))
This is done for each of the statuses so the table looks something like this:
PROJECTID PROJECTNAME INTAKEDATE SCOPEATE BUILDDATE TESTDATE GOLIVEDATE INTAKEDAYS SCOPEDAYS BUILDDAYS TESTDAYS
If there is any Macro or better formula someone could help me figure out, I'd appreciate it. There is also another field that gives the current status or if the project is considered live if that helps at all. I have excel-block right now and cant think of anything that makes sense for this.
So I figured it out using MIN and by adjusting the IF formulas a bit. Whoever it was that posted about MAX earlier, really helped me out getting me down this path. Kudos to you sir/madam.
I had to add the check to see if SCOPEDATE=BUILDDATE because without that it was returning 1 if the dates matched, with was adding one more day when compared to running the networkdays from INTAKEDATE to GOLIVEDATE.
=IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",
IF([#SCOPEDATE]="","",
IF([#SCOPEDATE]=[#BUILDDATE],0,
IF(NETWORKDAYS([#SCOPEDATE],(MIN([#BUILDDATE],[#TESTDATE],[#GOLIVEDATE])))<0,NETWORKDAYS([#SCOPEDATE],TODAY()),NETWORKDAYS([#SCOPEDATE],(MIN([#BUILDDATE],[#TESTDATE],[#GOLIVEDATE])))))))
I hope this is helpful for someone else.

Access 2013: How to Return All Records With Date Older than X?

My Problem
I have a date field in the table in question. I'm trying to create a query that will only display records where that date field are 120 days old or older.
My Code
Solution Attempt 1
The first solution I tried was simply added criteria to my date field. The criteria formula I used was:
< Date()-120
This removed a few 'random' records leaving me with 714 of my original 905 records. Unfortunately, a quick look through the dates of remaining records in ascending order, it was obvious that I was still getting records more recent that 120 days old.
Based on my 'random' result above, I double checked the format of my field by changing the field to:
DATE: Format([myDtField], "mm/dd/yyyy")
I ran the query again - this reduced the number of records removed, leaving me with 734 of 905 - but my issue persisted - I still had records with dates more recent than 120 old.
Solution Attempt 2
Based on my issues above, I decided to go a different route. This time I created a unique field for my criteria calculation. For the field value I used:
DateDiff: DateDiff("d",[myDtField],Date())
This resulted in values that were very far off from the correct values (ex. a record with a date of yesterday resulted in 43!).
Solution Attempt 3
This was less of a solution and more just troubleshooting, but based on the results I'm getting, I keep thinking my dates aren't being perceived by the system as the date it's displaying (i.e. DateValue() is off compared to displayed value). I spot checked a few of the dates vs their DateValue() and the randomly selected records all seemed to be correct. So again, no luck.
Solution Attempt 4
#Gustov reminded me about the DateValue() function. I've attempted that on the field as well - the field is imported as a text field, thus needs converted to a date value. Similar to the formula Gustov posted, for my field value I used:
DATEDIFF: DateDiff("d",Nz(DateValue([LASTPAYDT]),0),Date())
And then in the criteria I simply used the formula:
>120
This results in the following error:
Data type mismatch in criteria expression
This was probably the closest solution I had, simply because WITHOUT criteria it returns the proper values (i.e. a date of yesterday returned "1", while a date of two days ago returned "2"... etc). So you would think simply limiting records where this field is >120 would work, but then it throws up the error above.
My Question
Does anyone have an idea of how to check if a record field is 120
days old or older?
If either of my above solutions works for question 1, then what might be wrong with my date field that is causing issues if I am going about it the right way?
#Gustov's DateValue() solution is close (i.e. provides the correct values), but adding criteria causes an error. Any solutions?
I'm at a loss. An extra set of eyes on this problem would be greatly appreciated. Thanks!
Hans touches something. If the dates are not dates but text, try this:
Select * From YourTable
Where
IsDate([myDtField])
And
DateDiff("d", DateValue([myDtField]), Date()) > 120
I have tried this and it works, DATAText is the data stored as a text.
SELECT Tabella1.ID, Tabella1.DATAText, Date() AS Espr1
FROM Tabella1
WHERE (cDate(Format(Tabella1.DataText,"yyyy-MM-dd hh:mm:ss")))<Date()-120;

pulling current date queue

I have a view that lists employee (EmpID), request number (ReqNo), date request was opened (OpenDate) and the date it was moved to the next step in the process (AssignDate). What I am trying to do is get an average of the daily queue size. If EmpID 001 has 20 requests on 1/1/13, then has 24 on 1/2/13, 21 on 1/3/13 the average over 3 days should be 21.66, rounded up to 22. I have the following view:
CREATE VIEW EmpReqs
AS
SELECT [EmpID], [OpenDate], [AssignDate], [ReqID]
FROM [Metrics].[dbo].[Assignments]
WHERE OpenDate BETWEEN '01/01/2013' AND '12/31/2013' AND
[EmpID] IS NOT NULL AND
[ReqNo] NOT LIKE 'M%'
I then wrote a query to pull individual employee's queues per day:
/* First attempt to generate daily queue #s */
SELECT * FROM BLReqs
WHERE [BusLiaison] LIKE 'PN' AND
[OpenDate] <= '11/15/2013' AND
[AssignDate] > '11/15/2013'
Because no one has attempted to pull this information before, I have no way of verifying how accurate the above is. I tried using current dates, since I can see those in our database to compare but the code doesn't work, nothing is returned when I change the dates to 2014 and run my query.
What is the easiest way to verify that my code is correct, short of manually counting a day's queue?
Can anyone see any issues with the above scripts?
Is there a way to get the above code to work with current dates?
This question is really hard to answer because it is kind of broad and has little information at the same time. I'll try anyway:
Because no one has attempted to pull this information before, I have
no way of verifying how accurate the above is.
Try checking the result of this query for a few sampled dates.
I tried using current dates, since I can see those in our database to
compare but the code doesn't work, nothing is returned when I change
the dates to 2014 and run my query.
So clearly, the query is not working. You should probably find out why. Run the query for a date of which you know that it should return results but doesn't. Remove conditions one by one to see which one is incorrectly removing all rows. This should be enough to identify the bug.
Can anyone see any issues with the above scripts?
No, looks fine. A very simple query. That's why I said that we have too little information. There is some key piece of information missing that allows us to find the bug.
Is there a way to get the above code to work with current dates?
Stop staring at the code and hoping for a revelation. Debug it. Experiment.

Opening Hours Database Design

We are currently developing an application in which multiple entities have associated opening hours. Opening hours may span multiple days, or may be contained within a single day.
Ex. Opens Monday at 6:00 and closes at Friday at 18:00.
Or
Opens Monday at 06:00 and closes Monday at 15:00.
Also, an entity may have multiple sets of opening hours per day.
So far, the best design I have found, is to define an opening hour to consist of the following:
StartDay, StartTime, EndDay and EndTime.
This design allows for all the needed flexibility. However, data integrity becomes an issue. I cannot seem to find a solution that will disallow overlapping spans (in the database).
Please share your thoughts.
EDIT: The database is Microsoft SQL Server 2008 R2
Consider storing your StartDay and StartTime, but then have a value for the number of hours that it's open. This will ensure that your closing datetime is after the opening.
OpenDate -- day of week? e.g. 1 for Monday
OpenTime -- time of day. e.g. 08:00
DurationInHours -- in hours or mins. e.g. 15.5
Presuming a robust trigger framework
On insert/update you would check if the new start or end date falls inside of any existing range. If it does then you would roll back the change.
CREATE TRIGGER [dbo].[mytable_iutrig] on [mytable] FOR INSERT, UPDATE AS
IF (SELECT COUNT(*)
FROM inserted, mytable
WHERE (inserted.startdate < mytable.enddate
AND inserted.startdate > mytable.startdate)
OR (inserted.enddate < mytable.enddate
AND inserted.enddate > mytable.startdate)) > 0
BEGIN
RAISERROR --error number
ROLLBACK TRANSACTION
END
Detecting and preventing overlapping time periods will have to be done at the application level. Of course you can attempt to use a trigger in the database but in my opinion this is not a database issue. The structure that you came up with is fine, but your application logic will have to take care of the overlap.
There's an article by Joe Celko on the SimpleTalk website, over here, that discusses a similar issue, and presents am elegant if complex solution. This is probably applicable to your situation.
A table with a single column TimeOfChangeBetweenOpeningAndClosing?
More seriously though, I would probably not worry too much about coming up with a single database structure for representing everything, eventually you'll probably want want a system involving recurrences, planned closures etc. Persist objects representing those, and then evaluate them to find out the closing/opening times.
This looks like a good solution, but you'll have to write a custom validation function. The built in database validation (i.e. unique, less than x, etc.) isn't going to cut it here. To ensure you don't have overlapping spans, every time you insert a record into the database, you're going to have to select existing records and compare...
First the logic, two spans will overlap if the start value of one falls between the start/end of the other. This is much easier if we have datetimes combined, instead of date1,time1 and date2,time2. So a query to find an overlap looks like this.
select openingId
from opening o1
join opening o2 on o1.startDateTime
between o2.startDateTime
AND o2.endDateTime
You can put this into a trigger and throw an error if a match is found.