--This query is to find the oldest month of our given dataset on the table Datalog2. This is achieved by our two subqueries which determine our StartDate and EndDate.
--StartDate – It calculates the start date of the oldest month by finding the minimum (oldest) timestamp in the table, truncating it to the beginning of that month, and using that as the start date.
--EndDate – It calculates the end date of the oldest month by adding 1 month to the start date and then truncating it to the beginning of that month. This effectively gets the first date of the next month, which serves as the end date of the oldest month.

USE [ION_Data]

GO

DECLARE @StartDate datetime = (SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, MIN(TimestampUTC)), 0) FROM [ION_Data].[dbo].[DataLog2])

DECLARE @EndDate datetime = (SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, MIN(TimestampUTC)) +3, 0) FROM [ION_Data].[dbo].[DataLog2])

EXEC [ION_Data].[dbo].[TrimRecords]

 @StartDate

 , @EndDate

 , @TrimDataLog = 1

 , @TrimWaveForm = 1

 , @TrimEvent = 1