Empty or Null dates in SharePoint 2010
July 17, 2012 1 Comment
Turns out that my first, “simple” foray into SharePoint Workflows already hit upon a snag.
How do you test for an empty date in SharePoint 2010?
I did some digging and found that the common consensus was to convert the date to a string (using String Builder) and then use string comparison. Unfortunately, this didn’t work quite as expected.
Attempt 1: Test Date.“as String” begins with “1/1/0001”
Unfortunately, I don’t know enough about SharePoint to understand why this didn’t work. Through playing around, I found that if I used String Builder to convert an empty date to a string, the resulting string would be “1/1/0001 12:00:00 AM”. So, logic would seem to indicate that all I had to do was test to see if a string begins with “1/1/0001.” However, this did not work! Maybe someone could tell me why.
Here’s what I did:
- Set Variable: Date as String to [%Current Item: Cutoff Date%]
- Note: In String Builder, I set Return field as to As String
- If Variable: Date as String begins with 1/1/0001
- Log Date is empty. to the workflow history list
And, of course, the “Date is empty.” message never appeared in the workflow history.
I never did find out why “begins with” wasn’t operating as expected. I even changed the test to “does not begin with” and my logged output to be “Date does not start with 1/1/0001. Its value is ‘[%Variable: Date as String%]’.” Not surprisingly at this point, this time I DID get a message and it stated:
Date does not start with 1/1/0001. Its value is ‘1/1/0001 12:00:00 AM’.
Someone explain THAT one to me. Nevertheless, I persisted.
Attempt 2: Test Date.“Short Date” equals “1/1/0001”
Obviously, it occurred to me that I could test the value of the entire string. But while I was at it, I tried using the “Short Date” setting in String Builder.
I was then able to use essentially my original logic, and this time it worked!
- Set Variable: Date as String to [%Current Item: Cutoff Date%]
- Note: In String Builder, I set Return field as to Short Date
- If Variable: Date as String equals 1/1/0001
- Log Date is empty. to the workflow history list
- This one worked! There must be something I’m missing in the “begins with” operator. But for now, I have a solution to my problem.
what I do is just test for date< 1/1/1970 and if it is I assume it is NULL