Search code examples
u-sql

Extract Date from DateTime in U-SQL


How to extract date from datetime in U-SQL as there is no inbuilt function for it like SQL. Please find below my code

CREATE VIEW IF NOT EXISTS dbo.Para AS
EXTRACT Statements

 FROM "/FileName.csv"
    USING Extractors.Csv(quoting : true, nullEscape : "/N");    

@ABC =
    SELECT (createdOn).ToString("MM-dd-yyyy") AS Date
    FROM Para;

OUTPUT @ABC
TO "/date.csv"
USING Outputters.Csv();

Thanks in Advance


Solution

  • Note that U-SQL Types are .Net/C# datatypes that give you all the methods available to them in .Net (see https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx).

    So you can extract the date from datetime with:

    @ABC =
        SELECT createdOn.Date AS Date
        FROM Parameter;
    

    As an aside: Why are you specifying a view? Unless you want to register the view for later reuse in other scripts, you can just give the EXTRACT expression a name. E.g.,

    @input = EXTRACT ....