Date Functions
SQL for CRM Analytics supports DateTime, DateOnly, and Date date types. If you’re familiar with standard SQL, the DateOnly type corresponds with SQL’s Date type and the DateTime type corresponds with SQL’s Timestamp type. DateTime and DateOnly have custom time zone support. If your org doesn’t support custom time zones, then use the Date type, which supports GMT date information only.
A DateTime date type follows this format.
A DateOnly date type follows this format.
-
Use the
EXTRACT()function to access parts of a date. You can useEXTRACT()in projections, filtering, grouping and ordering. -
To project a date field of type
DateTime, use theEXTRACT()function on the date field in theSELECTstatement. -
Filter By Date Parts or Date Field
To filter by a date part, use the
WHEREclause with theEXTRACT()function, and pass it the desired date parts (year, month, or day). To filter by a date field, use theWHEREclause with a logical operator. -
To group by date part, use the
GROUP BYclause and theEXTRACT()function. PassEXTRACT()the date parts to isolate. -
To order by date part, use
EXTRACT()on the date field with theORDER BYclause. The query returns the count of rows containing these same values and orders the results by count descending. -
Project a Custom Fiscal Date Part
To project a custom fiscal date part, pass custom fiscal date parts to the
EXTRACT()function. -
Day in Week, Month, Quarter or Year Functions
Use the following functions to find the position of a day within a week, month, quarter, or year.
-
First and Last Day in the Week, Month, Quarter or Year Functions
Use the following functions to find the first and last week, month, quarter, or year for both standard and fiscal calendars. These functions accept
DateTime,DateOnly, and legacy input values. They return the same type as the input value.