Saturday, 10 September 2016

Time Conversions

CREATE COMPUTE MODULE TimeConvertion_Compute
    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
       
        DECLARE now CHARACTER;
        DECLARE target TIMESTAMP;
        Declare SampleDate1 CHARACTER '2016-05-06T13:53:12.832Z';
        DECLARE DateFrmtpattern CHARACTER 'MM/dd/yyyy hh:mm';
        ---CHARACTER to DATE-----
        SET target = CAST(SUBSTRING(REPLACE(SampleDate1,'T',' ') BEFORE 'Z') AS TIMESTAMP);   
        SET now = CAST(target AS CHARACTER FORMAT DateFrmtpattern);
        SET OutputRoot.XMLNSC.Casting.Date1=now;
       
        SET now = CAST(CURRENT_TIMESTAMP AS CHARACTER FORMAT 'yyyyMMdd-HHmmss');
        SET OutputRoot.XMLNSC.Casting.Date2=now;
       
        --CHARACTER to DATE----
        DECLARE SampleDate2 CHARACTER '31-02-2016';
        DECLARE target1 DATE;
        DECLARE DateFrmtpattern1 CHARACTER 'dd-MM-yy';
        SET target1 = CAST(SampleDate2 AS DATE FORMAT DateFrmtpattern1);
        SET OutputRoot.XMLNSC.Casting.Date3=target1;

        ----DECIMAL to CHARACTER----
        DECLARE SampleDecimalChar DECIMAL 1562.189;
        DECLARE DecCast CHARACTER;
        DECLARE DecFrmtpattern CHARACTER '#,##0.00';
        SET DecCast = CAST(SampleDecimalChar AS CHARACTER FORMAT DecFrmtpattern);
        SET OutputRoot.XMLNSC.Casting.Decimal=DecCast;
       
        ---CHARACTER to TIMESTAMP---
        DECLARE SampleTS CHARACTER '17 Jan 16, 3:45pm';
        DECLARE ts TIMESTAMP;
        DECLARE TSpattern CHARACTER 'dd MMM yy, h:mma';
        SET ts = CAST(SampleTS AS TIMESTAMP FORMAT TSpattern);
        SET OutputRoot.XMLNSC.Casting.TimeStamp=DecCast;
       
        RETURN TRUE;
    END;

END MODULE;

No comments:

Post a Comment