azuredateu-sql

Getting the 1st date of the current and previous month in U-SQL


How to get in U-SQL:

If I was using SQL I would write the following query (any idea how to write it in U-SQL?):

WHERE MyDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) AND DATEADD(MONTH, DATEDIFF(MONTH, 0, getdate()), 0)


Solution

  • You can use C# expressions for that:

    DECLARE @startDayThisMonth DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    DECLARE @startDayPreviousMonth DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 1);
    
    @data = 
        SELECT 
            x
         FROM 
            y
        WHERE
            MyDate BETWEEN @startDate AND @endDate;
    

    Examples can be found at this site.