SQL Server guide

List events for next week using dynamic dates


This query uses dynamic date functions to list events for the next week. <> represents today's date in datetime format, and <> adds 7 days. This allows for automated queries that always fetch the upcoming week's events without hardcoding dates.

Dynamic Placeholders: Supported functions include FUN_TO_DATE for dates and FUN_TO_DATETIME for timestamps.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT * FROM Events
--FROM EventsAll --(Get from All Calendars)
WITH (CalendarId='primary',
--//e.g. append -05:00 for EST (New York Time Zone) to convert local time to UTC -OR- just appends "Z" for UTC time (2026-01-01T05:00:00Z which is same as 2026-01-01T00:00:00-05:00 ).
      StartTime='<<today,FUN_TO_DATETIME>>-05:00',   
      EndTime='<<today+7d,FUN_TO_DATETIME>>-05:00')

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_GOOGLE_CALENDAR_IN_GATEWAY], 'SELECT * FROM Events
--FROM EventsAll --(Get from All Calendars)
WITH (CalendarId=''primary'',
--//e.g. append -05:00 for EST (New York Time Zone) to convert local time to UTC -OR- just appends "Z" for UTC time (2026-01-01T05:00:00Z which is same as 2026-01-01T00:00:00-05:00 ).
      StartTime=''<<today,FUN_TO_DATETIME>>-05:00'',   
      EndTime=''<<today+7d,FUN_TO_DATETIME>>-05:00'')')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_GOOGLE_CALENDAR_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM Events
--FROM EventsAll --(Get from All Calendars)
WITH (CalendarId=''primary'',
--//e.g. append -05:00 for EST (New York Time Zone) to convert local time to UTC -OR- just appends "Z" for UTC time (2026-01-01T05:00:00Z which is same as 2026-01-01T00:00:00-05:00 ).
      StartTime=''<<today,FUN_TO_DATETIME>>-05:00'',   
      EndTime=''<<today+7d,FUN_TO_DATETIME>>-05:00'')'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_CALENDAR_IN_GATEWAY]