Skip navigation
22546 Views 4 Replies Latest reply: Apr 13, 2011 12:20 PM by Shawn Fox RSS
myettam New Enzee 4 posts since
Nov 20, 2009
Currently Being Moderated

Jan 18, 2010 9:47 AM

date_trunc in Netezza

I am using Netezza date_trunc function but its giving different result compare to oracle.
Trunc function in Oracle gives the output as for ex
select sysdate from dual - 18-JAN-10 09.26.53
select trunc(sysdate) from dual - 18-JAN-10 12.00.00 (Its rounding to 12)

I need the same output using Netezza.

I have used the date_trunc function and its giving the different output.

select date_trunc(current_date) - 2010-01-18 09:00:00.I want the output as(2010-01-18 12:00:00).
Please help me in solving this issue.

  • Shawn Fox Enzee Exraordinaire 1,482 posts since
    Aug 15, 2006
    Currently Being Moderated
    1. Jan 18, 2010 10:03 AM (in response to myettam)
    Re: date_trunc in Netezza

    trunc(sysdate) in Oracle sets the time to midnight (not 12:00).  I am guessing that you have your default date format set to display the hours component of a time as 0 through 12 instead of 0 through 24.

     

    The main thing to understand is that Oracle does not have an ANSI compliant DATE datatype.  The DATE datatype in Oracle is actually a timestamp (it has hours/minutes/seconds), that is why you see the common practice of trunc(sysdate) in Oracle to get around Oracle's non standard DATE datatype.  In Netezza, current_date is a true SQL standard DATE datatype (it doesn't have hours/minutes/seconds), so you do not need to apply TRUNC to current_date at all.

     

    You should be able to replace trunc(sysdate) with current_date in Netezza.

     

    If you really need a timestamp instead of a date, you can use "cast(current_date as timestamp)" and that should yield the same results you are expecting

  • Superuser Rookie 90 posts since
    Sep 19, 2008
    Currently Being Moderated
    2. Jan 18, 2010 10:24 AM (in response to Shawn Fox)
    Re: date_trunc in Netezza

    tats true, but i am wondering how come select date_trunc(current_date) worked without any error? the actuall syntax is

    select date_trunc(units, col).



  • Hemachandra Ayodhya New Enzee 49 posts since
    Jan 28, 2010
    Currently Being Moderated
    3. Sep 3, 2010 11:47 AM (in response to Superuser)
    Re: date_trunc in Netezza

    I don't know how you have tried the query.I'm still getting an error saying "  FUNCTION DATE_TRUNC(DATE) does not exist "

     



  • Shawn Fox Enzee Exraordinaire 1,482 posts since
    Aug 15, 2006
    Currently Being Moderated
    4. Apr 13, 2011 12:20 PM (in response to Hemachandra Ayodhya)
    Re: date_trunc in Netezza

    There is no function named "date_trunc" that takes a single argument.  The date_trunc function takes two arguments:

     

    select date_trunc('day', current_timestamp)

     

    If you want to create a custom function that does the same thing as the Oracle trunc(sysdate) function,  here is an example of creating one using nzLua (works on any 5.x or 6.x system).  nzLua can be obtained by emailing netezza-nzlua@wwpdl.vnet.ibm.com or by joining the netezza developer network at www.netezza.com/ndn and searching for nzlua.  Note that when joining NDN you should use a real work email address, not a personal address such as gmail,  yahoo, etc.  Otherwise account creation may take a long time (or may never happen at all).

     

    [nz@netezza ~]$ cat trunc.nzl


    function evaluate(ts)
        return date_trunc('day',ts)
    end


    function getName()
        return "trunc"
    end


    function getType()
        return "udf"
    end


    function getArgs()
        args = {}
        args[1] = { "ts", timestamp }
        return args
    end


    function getResult()
        return timestamp
    end



    [nz@netezza ~]$ nzl trunc.nzl

     

    Compiling: trunc.nzl
    ####################################################################
    UdxName     =  trunc
    UdxType     =  UDF
    Arguments   =  TIMESTAMP
    Result      =  TIMESTAMP
    Dependencies = LIBNZLUA
    NZUDXCOMPILE OPTIONS: (--unfenced --mem 10m )
    CREATE FUNCTION
    Created udf
    Done



    [nz@netezza ~]$ nzsql -c "select trunc(current_timestamp)"


            TRUNC
    ---------------------
    2010-08-29 00:00:00
    (1 row)

More Like This

  • Retrieving data ...

Bookmarked By (0)