Search code examples
u-sql

Format Date Column in USQL


I currently have the following date format: "2017-Jan-30 12:45:02:345 EST"

But I need the format to be "MM/dd/yyyy HH:mm:ss"

Does anyone know how to do this? The issue is in the Date string below

@input= 
   EXTRACT
     Date string,
     Name string,
     Location string
   FROM @in
   USING Extractor.Csv();

OUTPUT @input
TO @out
USING Outputters.Csv();

Solution

  • You can do something like this:

    @input=EXTRACT
    [Date] String,
    [Name] string,
    [Location] string
    FROM @in
    USING Extractors.Csv();
    

    and then in SELECT statement you format your Date column:

    @result = SELECT 
    DateTime.ParseExact([Date],"yyyy-MMM-dd hh:mm:ss:fff EST",null).ToString("MM/dd/yyyy HH:mm:ss") AS Date
    Location,
    Name
    FROM @input
    

    For more DateTime formats in c# you can check this