In DataStage, date functions are essential for manipulating dates and times in a variety of ways. This article provides an overview of the most commonly used date functions in DataStage.
The Current_Date()
function returns the current system date. Here's a simple example:
StreamStage currentDateStage = new StreamStage("Current Date Stage");
TransformModule currentDateTrans = currentDateStage.newTransformModule("Current Date Transformer");
currentDateTrans.setExpression("Output(Current_Date())");
currentDateStage.setInput(inputStream)
.addTransform(currentDateTrans)
.setOutput(outputStream);
The Add_Months(date, number)
function adds a specified number of months to a given date. For example:
StreamStage addMonthsStage = new StreamStage("Add Months Stage");
TransformModule addMonthsTrans = addMonthsStage.newTransformModule("Add Months Transformer");
addMonthsTrans.setExpression("Output(Add_Months(Input(), 3))");
addMonthsStage.setInput(inputStream)
.addTransform(addMonthsTrans)
.setOutput(outputStream);
The Add_Years(date, number)
function adds a specified number of years to a given date. For example:
StreamStage addYearsStage = new StreamStage("Add Years Stage");
TransformModule addYearsTrans = addYearsStage.newTransformModule("Add Years Transformer");
addYearsTrans.setExpression("Output(Add_Years(Input(), 5))");
addYearsStage.setInput(inputStream)
.addTransform(addYearsTrans)
.setOutput(outputStream);
DataStage provides various extract functions to get specific parts of a date. For example, Year(date)
, Month(date)
, Day(date)
, Hour(date)
, Minute(date)
, and Second(date)
. Here's an example using the Year()
function:
StreamStage yearStage = new StreamStage("Year Stage");
TransformModule yearTrans = yearStage.newTransformModule("Year Transformer");
yearTrans.setExpression("Output(Year(Input()))");
yearStage.setInput(inputStream)
.addTransform(yearTrans)
.setOutput(outputStream);
The Format_Date(date, format)
function formats a date according to the specified pattern. For example:
StreamStage formatDateStage = new StreamStage("Format Date Stage");
TransformModule formatDateTrans = formatDateStage.newTransformModule("Format Date Transformer");
formatDateTrans.setExpression("Output(Format_Date(Input(), 'YYYY-MM-DD'))");
formatDateStage.setInput(inputStream)
.addTransform(formatDateTrans)
.setOutput(outputStream);