Date Manipulation inside a "SqlQuery" Statement

The SqlQuery statement lets you join and combine the resulting data from other statements by using SQL Select syntax. Enesys RS Data Extension uses SQLite SQL syntax. You can find detailed information about the date and time functions on the following web page.

This article provides samples and additional information in the context of Enesys RS Data Extension.

Date Comparison

For comparing dates inside a SqlQuery statement, you should use DATE or DATETIME functions to avoid any side effects.

Comparing dates without times

SELECT *
FROM Orders
WHERE DATE(OrderDate) >= DATE('1998-05-05 05:05:05')

you can use a parameter (or some other field) instead of a literal:

SELECT *FROM OrdersWHERE DATE(OrderDate) >= DATE(@StartDate!)

Comparing dates including times

SELECT *
FROM Orders
WHERE DATE(OrderDate) >= DATE('1998-05-05 05:05:05')

you can use a parameter (or some other field) instead of a literal:

SELECT *FROM OrdersWHERE DATETIME(OrderDate) >= DATETIME(@StartDate!)

Note that, for performance reasons, you should rather filter at the list level whenever possible.

Date Function Samples

In this chapter we will provide examples of most date functions available. In some cases, we will also provide the SQL Server counterpart.

Current Date

DescriptionSQLite functionSQL Server equivalent
Current date as UTCDATETIME(‘now’)GETUTCDATE()
Current local dateDATETIME(‘now’, ‘localtime’)GETDATE()

Specific parts of a date

For the purpose of illustrating the following examples, we will consider that dateField has the following value: 2011-02-11 13:50:10.

DescriptionSQLite functionSQL Server equivalent
Year (e.g. 2011)STRFTIME(‘%Y’, dateField)YEAR(dateField)
Quarter (e.g. 1)( STRFTIME(‘%m’, dateField) + 2) / 3DATEPART(QUARTER, dateField)
Month number (e.g. 02)STRFTIME(‘%m’, dateField)MONTH(dateField)
Month name (e.g. February)CASE STRFTIME(‘%m’, dateField) WHEN ‘01’ THEN ‘January’ WHEN ‘02’ THEN ‘February’ WHEN ‘03’ THEN ‘March’ WHEN ‘04’ THEN ‘April’ WHEN ‘05’ THEN ‘May’ WHEN ‘06’ THEN ‘June’ WHEN ‘07’ THEN ‘July’ WHEN ‘08’ THEN ‘August’ WHEN ‘09’ THEN ‘September’ WHEN ‘10’ THEN ‘October’ WHEN ‘11’ THEN ‘November’ ELSE ‘December’ ENDDATENAME(MONTH, dateField)
day of the year (e.g. 052)STRFTIME(‘%j’, dateField)DATEPART(DAYOFYEAR, dateField)
day of the month (e.g. 21) STRFTIME(‘%d’, dateField)DAY(dateField)
week number (e.g. 9)STRFTIME(‘%W’, dateField) + 1DATEPART(WEEK, dateField)
Day name (e.g. Monday)CASE STRFTIME(‘%w’, dateField) WHEN ‘1’ THEN ‘Monday’ WHEN ‘2’ THEN ‘Tuesday’ WHEN ‘3’ THEN ‘Wednesday’ WHEN ‘4’ THEN ‘Thursday’ WHEN ‘5’ THEN ‘Friday’ WHEN ‘6’ THEN ‘Saturday’ ELSE ‘Sunday’ ENDDATENAME(WEEKDAY, dateField)
Hour (e.g. 13)STRFTIME(‘%H’, dateField)DATEPART(HOUR, dateField)
Minutes (e.g. 50)STRFTIME(‘%M’, dateField)DATEPART(MINUTE, dateField)
Seconds (e.g. 10)STRFTIME(‘%S’, dateField)DATEPART(SECOND, dateField)

You can also combine SQLite format strings:

DescriptionSQLite function
MonthYear (e.g. 022011)STRFTIME(‘%m%Y’, dateField)
MonthYear (e.g. 0211)STRFTIME(‘%m’, dateField) |
Month-Year (e.g. 02-2011)STRFTIME(‘%m-%Y’, dateField)
YearMonthDay (e.g. 20110221)STRFTIME(‘%Y%m%d’, dateField)

It should be noted that the STRFTIME function always return a string unlike the SQL Server counter part that return a numeric value. Therefore, if you would like the same behavior as the SQL Server counterpart, and for comparison purposes, you may want to convert the result to an int by using the CAST function as shown below:

SELECT * FROM Foo WHERE CAST(STRFTIME('%Y', dateField) AS INT) = 2011

In that case, comparing with a string would also work:

SELECT * FROM Foo WHERE STRFTIME('%Y', dateField) = '2011'

Difference between two dates

We have to admit SQLite approach is not extremely natural. It is shown here for the sake of completeness. However, unless you need it, we would rather recomment using a DATEDIFF function directly within a Reporting Services expression.

DescriptionSQLite functionSQL Server equivalent
Difference in days (e.g. 16)CAST((JULIANDAY(endDate) – JULIANDAY(startDate)) AS INT)DATEDIFF(DAY, startdate, enddate)

To get the number of hours or minutes, just multiply the result by 24 or 1440.

Adding or Removing Time

DescriptionSQLite function
Adding 1 yearDATETIME(dateField, ‘+1 years’)
Adding 2 monthsDATETIME(dateField, ‘+2 months’)
Removing 10 daysDATETIME(dateField, ‘-10 days’)
Adding 5 hoursDATETIME(dateField, ‘+5 hours’)
Adding 30 minutesDATETIME(dateField, ‘+30 minutes’)

Date Shifting

Finally, SQLite syntax provides various modifiers that allow you to shift the date to some specific point in time like the start of the year. Note that you can use multiple modifiers for powerful date shifting. The following examples will certainly clarify:

DescriptionSQLite function
Start of the year (e.g. 2011-01-01 00:00:00)DATETIME(dateField, ‘start of year’)
Start of the month (e.g. 2011-02-01 00:00:00)DATETIME(dateField, ‘start of month’)
Start of the day (e.g. 2011-02-21 00:00:00)DATETIME(dateField, ‘start of day’)
Next Friday (e.g. 2011-02-25 13:50:10)DATETIME(dateField, ‘weekday 5’)
First Sunday of the month (e.g. 2011-02-06 00:00:00)DATETIME(dateField, ‘start of month’, ‘weekday 0’)
First day of next monthDATETIME(dateField, ‘+1 month’, ‘start of month’)
  • Applies to
    Enesys RS Data Extension 3
  • Last Updated
    2011-03-04T00:00:00.000Z
  • Categories
    How-to