Jan 18, 2010 9:47 AM
date_trunc in Netezza
-
Like (0)
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.
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
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).
I don't know how you have tried the query.I'm still getting an error saying " FUNCTION DATE_TRUNC(DATE) does not exist "
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)

