I’ve been tasked to take a calendar date range value from a form front-end and use it to, among other things, feed a query in a Teradata table that does not have a datetime column. Instead the date is aggregated from two varchar columns: one for year (CY = current year, LY = last year, LY-1, etc), and one for the date with format MonDD (like Jan13, Dec08, etc).
I’m using Coldfusion for the form and result page, so I have the ability to dynamically create the query, but I can’t think of a good way to do it for all possible cases. Any ideas? Even year differences aside, I can’t think of anything outside of a direct comparison on each day in the range with a potential ton of separate OR statements in the query. I’m light on SQL knowledge – maybe there’s a better way to script it in the SQL itself using some sort of conversion on the two varchar columns to form an actual date range where date comparisons could then be made?
Here is some SQL that will take the
VARCHARdate value and perform some basic manipulations on it to get you started:The
EXTRACT()function allows you to take apart aDATE,TIME, orTIMESTAMPvalue.You have you use
TRIM()around theEXTRACTto get rid of the whitespace that is added converting theDATEPARTto aCHARdata type. Teradata is funny with dates and often requires a doubleCAST()to get things sorted out.The
CASEstatement simply takes the encoded values you suggested will be used and uses theADD_MONTHS()function to manipulate the date. Dates areINTEGERin Teradata so you can also addINTEGERvalues to them to move the date by a whole day. Unlike Oracle, you can’t add fractional values to manipulate theTIMEportion of aTIMESTAMP.DATE!=TIMESTAMPin Teradata.