Solved

In T-SQL how to handle [Expiration Date] = 'perpetual' or 'month-to-month' or 'dates vary'

Posted on 2016-09-28
6
53 Views
Last Modified: 2016-10-19
I've been given 13 different spreadsheets that are currently used for tracking our companies contracts.  They look like they were created by 13 different monkeys. No consistency between them, no data consistency within each one, multiple data stored in single fields.  Those are the least of the issues with these Excel sheets. I have to import them into a MS SQL Server 2008 R2 table. /rant

Anyway... First question:  Given the [Excel] column "Expiration Date" and understanding that this date [in the db table] is used in a PowerShell script to send an email alert to the contact person x days prior to the contract expiring:
How would you handle the following text entries from the spreadsheet in that date column with respect to what exactly should go into the date column and how would the various conditions be handled for the expiration warning alert message?

Entries in "Expiration Date" column:
"6/30/2017"      <-- That one is good.  ;-)
"month to month"
"perpetual"
"expiration dates vary"
"12 month subscription"

Open in new window

Those are the actual values in the expiration date column.
I hate using date "codes" like 1/1/1900 or 12/31/9999 to represent information in a data field and I doubt if the users of the database once it's done would be able to manage that complexity.

How would you handle this? Thanks!
0
Comment
Question by:megnin
  • 2
  • 2
  • 2
6 Comments
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
Do you have ANY datetime column tied to the (customer) record to relate back to any of those entries?
If not then in my opinion is impossible to "translate" int SQL WHERE clause something like "month to month"...
Maybe "perpetual" is also "good" as that means in my (poor) English this never expire as long as Customer.valid=true but anything else is a joke in my opinion without a "contract start date" or "contract effective date" datime column.
0
 
LVL 1

Author Comment

by:megnin
Comment Utility
Just the 'Contract Expiration Date' column.  I'll have to have conversations with the owners of the spreadsheets.  After a recent conversation "perpetual" sometimes that it is "auto renewing" but may not be "indefinite."  Answers that they will have to provide to me.

You are right; I don't think this is going to be solved with a WHERE clause.  The data is going to have to be cleaned up by the people who what they meant.  Thank you for your input.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
Comment Utility
OK so if you have "Contract Expiration Date" as a date and certain strings stored in that "Expiration Date" column this can be still done using SQL CASE... syntax however those strings must be "unique" in a group - hope you understand what I am trying to say here.

Like they all must be like:
"12 month subscription" and NOT variations like "twelve month subscription" OR "12 months subscription" ETC for you to be able to build a CASE like pseudo code below:

...
CASE WHEN [Expiration Date] = "12 month subscription"  
              AND [Contract Expiration Date] - "12 mths" > getdate() - xdays
THEN -- send email
ELSE -- wait till next time check
END
.....
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You need to add an (internal) tinyint column to the table to store a code representing the [Expiration Date] (ED).  Thoroughly analyze the ED one time when it is initially entered, assign the code and then use the code from then on.  Having to analyze the ED every time you check it will be cost prohibitive.

My preference would be for a trigger to analyze it, upon INSERT or UPDATE, but the trigger needs to be written efficiently, and must be able to log something to notify someone if a truly bad value is entered.  The sooner to the time of entry that it's corrected the easier the correction will be.
0
 
LVL 1

Author Comment

by:megnin
Comment Utility
Icohan, thanks, I'll see if I can use the CASE WHEN with the existing data from the spreadsheets.  The owner of the spreadsheets is now doing some cleanup of those sheets.  We'll see what they look like once she's done.  ;-)

ScottPletcher, that does sound like a more efficient way to do the expiration checks.  Would checking the expiration date one time and assigning it an integer code also allow for updating the date once it expires and is renewed with a new date.  Most of these contracts are one to three years in length and are then renewed for another "term."
How would you "code" an expiration date as an integer?  Would you use number of days from a fixed reference date?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
The tinyint would simply be a code that represents a very-clearly-defined "EXpiration Date", i.e., with very clear rules about it works.

Sample "Expiration_Date_Code" column values with
    Explanation:
1 = "6/30/2017"
    A specific, valid date was entered; this date will be used as the exp date
2 = month to month, for a fixed period of time
    Expiration date is the same day every month, for a fixed number of months/years
92 = month to month, "forever"/until cancelled
93 = perpetual
    ?
8 = expiration dates vary
    ?
? = "12 month subscription"
    "12 month subscription" might become a type 2, with a one year fixed time period.

So, upon initial INSERT of the row, and for any UPDATE of "Expiration Date" (ED) afterward, the value is analyzed, matched to the available naming patterns, and a code assigned.  If the ED can't be converted to a code, then an error is raised immediately by whatever method you want to use.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now