Table of Contents

Class DateSubtract

Namespace
YndigoBlue.Velocity.Functions
Assembly
YndigoBlue.Velocity.dll

Represents a date/time arithmetic function that subtracts a number of date/time units from a date, datetime, or timestamp value.

public class DateSubtract : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
Inheritance
DateSubtract
Implements

Remarks

DateSubtract(DatePart, amount, date) → date shifted back by amount units.

The datePart argument specifies the unit of time to subtract (e.g. Year, Month, Day, Hour, Minute, Second). The amount must be a non-negative value; to add instead of subtract, use DateAdd.

The SQL generated is database-specific and is handled by each adaptor's BuildFunction override.

Examples

Calculate the start date by subtracting 30 days from each subscription expiry date:

var schema = manager.LoadSchema("billing");
var subscriptions = schema["subscriptions"];

var query = new Query()
    .Select([ subscriptions["id"], new Expression("start_date", new DateSubtract(DatePart.Day, 30, subscriptions["expiry_date"])) ])
    .From(subscriptions);

var results = manager.Retrieve(query);

Use the column + column constructor to subtract a variable number of days (stored in a column) from a date column:

var schema = manager.LoadSchema("projects");
var tasks = schema["tasks"];
Column endColumn = tasks["end_date"];
Column durationColumn = tasks["duration_days"];

var query = new Query()
    .Select([ tasks["id"], new Expression("start_date", new DateSubtract(DatePart.Day, durationColumn, endColumn)) ])
    .From(tasks);

var results = manager.Retrieve(query);

Subtract a fixed number of months from a hard-coded DateOnly literal:

var expiryDate = new DateOnly(2025, 12, 31);

// Go back 12 months from the literal date
var expr = new DateSubtract(DatePart.Month, 12, expiryDate);

Constructors

DateSubtract(DatePart, int, DateOnly)

Initializes a new instance of DateSubtract with an integer amount and a DateOnly literal.

DateSubtract(DatePart, int, DateTime)

Initializes a new instance of DateSubtract with an integer amount and a DateTime literal.

DateSubtract(DatePart, int, Column)

Initializes a new instance of DateSubtract with an integer amount and a date column.

DateSubtract(DatePart, IElement, IElement)

Initializes a new instance of DateSubtract.

DateSubtract(DatePart, Column, DateOnly)

Initializes a new instance of DateSubtract with a column amount and a DateOnly literal.

DateSubtract(DatePart, Column, DateTime)

Initializes a new instance of DateSubtract with a column amount and a DateTime literal.

DateSubtract(DatePart, Column, Column)

Initializes a new instance of DateSubtract with a column for both amount and date.

Properties

DatePart

Gets the date/time component unit used for the subtraction.