Solved

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

Posted on 2016-09-28
6
63 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
ID: 41820505
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
ID: 41820519
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
ID: 41820572
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41820587
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
ID: 41826701
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
ID: 41826773
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

937 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

4 Experts available now in Live!

Get 1:1 Help Now