Trunc function
Dear All,
what does this function trunc(date_column-.125) mean?
thanks
it subtracts 3 hours (.125 = 1/8 = 3/24) from the given date and sets the time part (hours, minutes, seconds) to 00:00:00.
select sysdate current_date
, sysdate -.125 current_date_minus_3h
, trunc(sysdate -.125) truncated_date
from dual
CURRENT_DATE CURRENT_DATE_MINUS_ TRUNCATED_DATE
06.01.2013 09:07:29 06.01.2013 06:07:29 06.01.2013 00:00:00It's described in the documentation and you can test it in every Oracle database.
Similar Messages
-
How to use TRUNC function with dates in Expression Builder in OBIEE.
Hi There,
How to use TRUNC function with dates in Expression Builder in OBIEE.
TRUNC (SYSDATE, 'MM') returns '07/01/2010' where sysdate is '07/15/2010' in SQL. I need to use the same thing in expression builder in BMM layer logical column.
Thanks in advanceuse this instead:
TIMESTAMPADD(SQL_TSI_DAY, ( DAYOFMONTH(CURRENT_DATE) * -1) + 1, CURRENT_DATE) -
ORA-00932 while using nvl with trunc function
What happens when trunc is executed on null?
I am executing the following query:
"select nvl(trunc(null),sysdate) from dual"
and this throws "ORA-00932: inconsistent datatypes: expected NUMBER got DATE, error at Line:1 Column:23".
Whereas "select nvl(null,sysdate) from dual" returns sysdate correctly.
Also "select trunc(null) from dual" returns null only. So when the returned null is passed through nvl why am I getting exception?
This is happening in ver 9.2.0.5.0 and 10.2.0.2.0
There is another observation, which is an issue that we have found in our code, and while trying to fix that we saw the earlier observation.
SELECT * FROM orgs, dual where
trunc(SYSDATE) between trunc(orgs.effective_start_date) and nvl(trunc(orgs.effective_end_date),trunc(sysdate))
Here effective start date and effective end date for orgs are null for all records.
When we run this query on ver 9.2.0.5.0, this runs without any exception. But when we run this query on ver 10.2.0.2.0, we get the same exception, "ORA-00932: inconsistent datatypes: expected NUMBER got DATE, error at Line:2 Column:95".
The join with dual is fake, in actual scenario we have join with other tables, but since we are able to replicate with dual, removed all other details to keep this simple.
Now if we remove the join with dual, the query works fine in both the env.
SELECT * FROM orgs where
trunc(SYSDATE) between trunc(orgs.effective_start_date) and nvl(trunc(orgs.effective_end_date),trunc(sysdate))What happens when trunc is executed on null?
I am executing the following query:
"select nvl(trunc(null),sysdate) from dual"
and this throws "ORA-00932: inconsistent datatypes:
expected NUMBER got DATE, error at Line:1
Column:23".
Whereas "select nvl(null,sysdate) from dual" returns
sysdate correctly.
Also "select trunc(null) from dual" returns null
only. So when the returned null is passed through nvl
why am I getting exception?
This is happening in ver 9.2.0.5.0 and 10.2.0.2.0The first parameter to NVL is determining the expected datatype of the returned column, with the trunc function defaulting that to NUMBER because it's parameter is NULL. The second parameter to NVL needs to match that datatype which it doesn't because it is a date.
SQL> select nvl(trunc(sysdate), sysdate) as mydate from dual;
MYDATE
26/05/2006 00:00:00
SQL> select nvl(trunc(null), sysdate) as mydate from dual;
select nvl(trunc(null), sysdate) as mydate from dual
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
SQL> select nvl(trunc(123), sysdate) as mydate from dual;
select nvl(trunc(123), sysdate) as mydate from dual
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
SQL> -
Another thread from today made me think of the basics of TRUNC function used for DATEs. I am confused about what it acutally does.
This is what Oracle Documenations says about TRUNC function without format (like 'DD-MON-YY')
"If you omit fmt, then date is truncated to the nearest day" (Oracle 10Gr2 SQL reference, B14200-02)
SQL> alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
Session altered.
Query 1.
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
06-DEC-2007 14:22:01
Query 2.
SQL> SELECT TRUNC(SYSDATE) FROM DUAL;
TRUNC(SYSDATE)
06-DEC-2007 00:00:00I have three questions
1)Since it 2:22 pm , shouldn't the result of TRUNC(SYSDATE) ie Query 2. be 7-Dec-2007 ?
2) What difference does TRUNC function make in the code apart from setting the time to 00:00:00?
3) Is it safer to add TRUNC for DATEs in the code?
SQL> select sysdate-1 from dual;
SYSDATE-1
05-DEC-2007 14:27:51
SQL> select trunc(sysdate) - 1 from dual;
TRUNC(SYSDATE)-1
05-DEC-2007 00:00:00Added third question
Message was edited by:
user609308it's not a function that rounds up date's it purely truncates to the granularity that you want.
So TRUNC(sysdate) still returns a date datatype (which still contains a date and time portion) but the time portion is truncated i.e. cleared down to it's lowest limit.
Likewise if you TRUNC(sysdate,'YYYY') then it will truncate to the year and take months, days, hours, mins and secs down to their lowest limit hence giving you 1st January of that year with 00:00:00 as the time.
It's very simple really. -
Using TRUNC function on partitioned column
Hi All,
I have a table as follows:
STEP1
CREATE TABLE TEST_PARTITION
EMP_ID VARCHAR2(10 BYTE),
CREATE_DT DATE,
EMP_RGN_NM VARCHAR2(2 BYTE),
DSPTCH_CNT NUMBER
PARTITION BY RANGE (CREATE_DT)
SUBPARTITION BY LIST(EMP_RGN_NM)
SUBPARTITION TEMPLATE(
SUBPARTITION RGN_E VALUES ('E') ,
SUBPARTITION RGN_MW VALUES ('MW') ,
SUBPARTITION RGN_SW VALUES ('SW') ,
SUBPARTITION RGN_W VALUES ('W') ,
SUBPARTITION RGN_SE VALUES ('SE')
PARTITION aug2008 VALUES LESS THAN (TO_DATE('01-Sep-2008', 'DD-MON-YYYY')),
PARTITION sep2008 VALUES LESS THAN (TO_DATE('01-Oct-2008', 'DD-MON-YYYY')),
PARTITION oth VALUES LESS THAN (MAXVALUE)
ENABLE ROW MOVEMENT;
STEP 2
insert into TEST_PARTITION values(1000,TO_DATE('01-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10)
insert into TEST_PARTITION values(1000,TO_DATE('02-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',20);
insert into TEST_PARTITION values(1000,TO_DATE('03-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',0);
insert into TEST_PARTITION values(1000,TO_DATE('01-sep-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
insert into TEST_PARTITION values(1000,TO_DATE('02-sep-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
insert into TEST_PARTITION values(1000,TO_DATE('01-Oct-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
insert into TEST_PARTITION values(1001,TO_DATE('01-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',1);
insert into TEST_PARTITION values(1001,TO_DATE('02-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',2);
insert into TEST_PARTITION values(1001,TO_DATE('03-Aug-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',0);
insert into TEST_PARTITION values(1001,TO_DATE('01-sep-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
insert into TEST_PARTITION values(1001,TO_DATE('02-sep-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',5);
insert into TEST_PARTITION values(1001,TO_DATE('01-Oct-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
insert into TEST_PARTITION values(1001,TO_DATE('02-Oct-2008 12:02:02', 'DD-MON-YYYY HH:MI:SS'),'SE',10);
STEP 3
I need to get all the dispatches on 1st of August and hence issue the statement as follows:
select * from test_partition where TRUNC(CREATE_DT)='01-Aug-2008' and EMP_RGN_NM = 'SE'
Using a function over the partitioned column, will it avaoid partition pruning? I mean will it scan all the partitiones instead of going to specific partition?
I need this urgently since we are having a discussion on this in few minutes from now.
Thanks so much
SaffWhat about a function based index ?
SQL> select * from test_partition where TRUNC(CREATE_DT)='01-Aug-2008' and EMP_RGN_NM = 'SE';
Execution Plan
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart| Pstop |
| 0 | SELECT STATEMENT | | 1 | 32 | 4 (0)| | |
| 1 | PARTITION RANGE ALL | | 1 | 32 | 4 (0)| 1 | 3 |
| 2 | PARTITION LIST SINGLE| | 1 | 32 | 4 (0)| KEY | KEY |
| 3 | TABLE ACCESS FULL | TEST_PARTITION | 1 | 32 | 4 (0)| KEY | KEY |
Note
- 'PLAN_TABLE' is old version
SQL> create index idx on test_partition (TRUNC(CREATE_DT));
Index created.
SQL> select * from test_partition where TRUNC(CREATE_DT)='01-Aug-2008' and EMP_RGN_NM = 'SE';
Execution Plan
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Pstart| Pstop |
| 0 | SELECT STATEMENT | | 1 | 32 | 2 (0)| | |
| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| TEST_PARTITION | 1 | 32 | 2 (0)| ROWID | ROWID |
| 2 | INDEX RANGE SCAN | IDX | 1 | | 1 (0)| | |
Note
- 'PLAN_TABLE' is old version
I need this urgently since we are having a discussion on this in few minutes from now.It is not our problem, but yours.
Nicolas. -
Using Trunc Function in OBIEE RPD to TRIM DATE
Below is my requirment.How can i achieve this in BMM layer.
CASE WHEN TRUNC(REPORTDATE) <= TRUNC(SYSDATE-2) THEN 1 ELSE 0 ENDTry this
case when cast(REPORTDATE as date)<=cast((current_date-2) as date) then 1 else 0 end
let me know updates
Thanks
http://cool-bi.com
Edited by: Srini VEERAVALLI on May 22, 2013 9:29 AM -
Guys,
About the query below, how is the output and how SQL treats that?
SELECT TRUNC(ROUND(156,00,-1),-1)
FROM DUAL
Thank you!Don't be afraid of breaking Oracle by actually trying things.
Break it into pieces and look at each piece one at a time.
Remove the TRUNC operation and see what you get. Do you understand why you get that result? If not, post it and we can help explain it.
But you need to make the effort yourself.
See the SQL Language doc. It has an example just like yours.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions142.htm
>
ROUND returns n rounded to integer places to the right of the decimal point. If you omit integer, then n is rounded to zero places. If integer is negative, then n is rounded off to the left of the decimal point.
The following example rounds a number one digit to the left of the decimal point:
SELECT ROUND(15.193,-1) "Round" FROM DUAL; Round ---------- 20
>
Which means your number, 156,00, will get rounded one digit to the left of the decimal point. So 156 gets rounded to 160.00 since the '6' is equal to or larger than '5' it gets rounded up.
That leaves a result of 160.00. The TRUNC operation does something similar excepts instead of rounding one place left it truncates. But since one place left already has a '0' there is nothing for TRUNC to do so you still have 160.00 -
TRUNC function gives wrong date
I am using the code
SELECT TRUNC(SYSDATE-v_days,'DAY') INTO :P33_FROM_DATE FROM DUAL;
where v_days is a multiple of 7 (this is used to move around weeks), this should give back the start day of the week (if done today it would give 14th), however for some reason in APEX if gives the sundays date. In SQL Dev it works fine and is reporting the Mondays date. The inconsistencies are strange, this is the only process on the page, any ideas whats causing it?
ThanksHi Elliot,
According to the SQL User's Guide,
"The starting day of the week used by the format models DAY, DY, and D is specified implicitly by the initialization parameter NLS_TERRITORY."
So I'd look at your application language settings.
- Marco -
when i use in my proceadure where trunc(timesent_acct) between '30-AUG-2006' and '27-SEP-2006' it returns 410 rows
but
when i use where timesent_acct between '30-AUG-2006' and '27-SEP-2006' it returns me 400 rows
what actually happened out there my data stored in timestamp like this '9/27/2006 10:24:18 AM'
what trunc actually do with dates
thanksHello,
Trunc() without parameter retire the hour part of a date.
SQL> alter session set nls_date_format='DD/MM/YYYY HH24:MI:SS'
2 /
Session modifiée.
SQL> select sysdate, trunc(sysdate) from dual
2 /
SYSDATE TRUNC(SYSDATE)
07/11/2006 14:51:04 07/11/2006 00:00:00
SQL> Francois -
Oracle function and query return different results
Hi, I am using oracle 10g database.
Function is :
create or replace FUNCTION FUNC_FAAL(myCode number,firstDate date
*, secondDate date)*
RETURN INTEGER as
rtr integer;
BEGIN
select count() into rtr*
from myschema.my_table tbl where tbl.myDateColumn between firstDate and
secondDate and tbl.kkct is null and tbl.myNumberColumn = myCode ;
return (rtr);
END FUNC_FAAL;
This function returns 117177 as result.
But if I run same query in the function seperately ;
select count()*
from myschema.my_table tbl
where tbl.myDateColumn between firstDate and secondDate
and tbl.kkct is null and tbl.myNumberColumn = myCode ;
I get different result 11344 (which is the right one).
Table and function are in the same schema.
What can be the problem ?
Thanks.1. i think ur parameter name and Column names are same Firstdate and seconddate try to choose different name
2. try using Trunc function around your dates
where trunc(tbl.myDateColumn) between trunc(firstDate) and trunc(secondDate)then compare the result....sometimes time elements comes into play.
Baig
[My Oracle Blog|http://baigsorcl.blogspot.com/] -
BETWEEN FUNCTION WITH IN A DECODE FUNTION IN A CURSOR
The following below is my query..I have to get the hours,min and seconds from a 'yyyy-mm-dd-24hh.mi.ss' value and check if the time is between 12 am to 6 am , then write it with one value else write an another value..I am trying to use 'BETWEEN' function in decode function but i am getting error.....Can we use BETWEEN function with in decode function or is there any other way
set serveroutput on
declare
cursor cur_dte is select lst_upd_date from EMPLOYESS ;
begin
for i in cur_dte loop
DECODE (substr(trim(i.lst_upd_date),12)) ,between '00.00.00.0000' and '06.00.00.00.0000' ,101,102);
dbms_output.put_line(i.lst_upd_date);
end loop;
endFirst of all. If you are in PL/SQL then CASE is just a more colmplex expression then IF THEN ELSE. I usually prefere If then else, but for some rare cases.
The other issue is that you convert a datetime value into a string. This is wrong. it opens up all possible kinds of cenversion bugs. Stay with date or timestamp as long as possible.
The solution depends a little upon the datatype of your lst_upd_date column.
Here is a pl/sql solution assuming it is DATE.
The TRUNC function can be used to reduce a datetime to a day or to an hour.
declare
cursor cur_dte is select lst_upd_date from employees ;
begin
for i in cur_dte loop
if trunc(i.lst_upd_date,'HH') between trunc(i.lst_upd_date) and trunc(i.lst_upd_date)+6/24 then
dbms_output.put_line(to_char(i.lst_upd_date,'DD-MON-YYYY HH24:MI:SS'));
end if;
end loop;
end;
/But a pure SQL solution is much better.
Here is how you implement it using CASE in SQL.
example using pure sql
select e.*,
case when trunc(e.lst_upd_date,'HH')
between trunc(e.lst_upd_date) and trunc(e.lst_upd_date)+6/24
then 101
else 102
end as "early_morning_check"
from employees e;And if it is a timestamp column then you could use the EXTRACT function.
select e.*,
case when to_number(extract(hour from e.lst_upd_date))
between 0 and 6
then 101
else 102
end as "early_morning_check"
from employees e;You might want to consider if date values like 06:45:00 should be included or not. -
I have a report that has the option of dispalying pdf or excel. I have the pdf version working fine but i am having a little trouble on the excel version. I have a grand total and department total. Basically I need help formating two decimal places. Both dept_total and grand_total are numbers that both display. I have another column that displays what percent of the total is from each dept.It currently returns a number such as 10.23383547957459973 which i need to display as 10.23 How can i incorporate the trunc function into this code?
...to_char((dept_total/grand_total) *100)
The query looks similar to this.
select
to_char(a._grand_total)
,to_char(a.dept_total)
,to_char((dept_total/grand_total) *100)
from(
select sum(xxxx) grand_total,
sum(xxxx) dept_total
from table)aUse ROUND:
select to_char(round(10.23383547957459973, 2)) from dual;
10.23 -
Trunc percentage two decimal places
I have a report that has the option of dispalying pdf or excel. I have the pdf version working fine but i am having a little trouble on the excel version. I have a grand total and department total. Basically I need help formating two decimal places. Both dept_total and grand_total are numbers that both display. I have another column that displays what percent of the total is from each dept.It currently returns a number such as 10.23383547957459973 which i need to display as 10.23 How can i incorporate the trunc function into this code?
...to_char((dept_total/grand_total) *100)
The query looks similar to this.
select
to_char(a._grand_total)
,to_char(a.dept_total)
,to_char((dept_total/grand_total) *100)
from(
select sum(xxxx) grand_total,
sum(xxxx) dept_total
from table)aHi,
Since you're calling TO_CHAR anyway, you can add a 2nd argument, specifying two decimal places:
select
a._grand_total
, a.dept_total
, to_char ((dept_total/grand_total) *100, '999999999.99') AS pct
from (
select sum (xxxx) grand_total,
sum (xxxx) dept_total -- Why have two copies of the same value?
from table
)a If you're not using a 2nd argument, why use TO_CHAR at all in this query?
This will round the value, not truncate it. That is, if the value is 10.23<b>5</b>83547957459973, it would display '10.2<b>4</b>', not '10.2<b>3</b>'. If you always want to round toward 0 (rather than to the nearest multiple of .01), then use TRUNC. -
ADF jdev10: SQL TRUNC with bind variable in sql statement in VO
Hi,
In VO in where clause:
TRUNC(ServiceRequest.REQUEST_DATE) > :MyDate works fine!
But TRUNC(ServiceRequest.REQUEST_DATE) > TRUNC(:MyDate) doesn't work!
"SQL Query Error Message: ORA-00932: inconsistient datatypes: expected DATE got NUMBER"
How to use TRUNC function for a bind variable?Basementjack,
Any of these solutions that you are doing have possible performance implications in the database side, as you are applying a function to a database column; this is generally going to preclude the use of an index.
Assuming that you are trying to find service requests that were before the date that is being passed in in the bind variable, and assuming that both the bind variable and service request date can have time components, you could try:
ServiceRequest.REQUEST_DATE <= TO_DATE(trunc(:MyDate) - 1/86400)Does this do what you want?
John -
I get this error message saying literal does not match format string every time i type in the simple query below
select trunc(t1.transaction_date) from table_done t1
where trunc(t1.transaction_date, 'YYYY-MM-DD') < '2006-01-01'
and trunc(t1.transaction_date, 'YYYY-MM-DD')>= '2006-12-31';
Kindly note, the main reason, I am doing a trunc is because the date in the table contains hours and mins and I only want to get the years, month and day as string charater for a particular range. Thank you.
I am still new to oracle. thank youIs t1.transaction_date already a DATE data type? If so all you need to do is apply the TRUNC() function with only the column as the argument which will remove the hour, minute and second components. It looks like some of your post got cut off so I'll guess at what it should be.
SELECT TRUNC(t1.transaction_date)
FROM table_done t1
WHERE trunc(t1.transaction_date) BETWEEN TO_DATE('2006-01-01','YYYY-MM-DD') AND TO_DATE('2006-12-31','YYYY-MM-DD')Additionally you shouldn't compare dates to strings as you have done. Always convert the strings to DATEs if you want to compare dates.
If t1.transaction_date isn't a DATE (always store DATEs as DATEs not strings!) then you'll need to apply the TO_DATE() function.
Maybe you are looking for
-
I have a copy of Windows 7, and I am trying to bring up Boot Camp on my MacBook with Snow Leopard. But my Snow Leopard install disk is too old for Windows 7, so attempted Boot Camp install of drivers fails. I have hand-installed the NVidia graphics d
-
How to limit number of mail messages on iPhone with iOS7?
Is there a way to limit the number of inbox email messages showing? On iOS6, it was in Settings under Mail but this option is not there on iOS7 Is it hiding somewhere else? If this isn't an option at all, are there any suggestions on a work around
-
i would like to know if apple has such a program that will let you trade i your computer for some cash or credit. EX: apple gives me 350 for my macbook and now i can get a new one for a bit less.
-
MBP as good as dead after upgrade to 5.8?
Hi All, A strange problem with my MBP after trying to upgrade to 5.8. It never restarted after a seemingly normal installation script. Only things happening are the sleeping led comming on, a short sounding noise from the fan, the cd-drive clicking a
-
Url differences between versions
when I was using OHS 10.1.3.. any folders that I create in my document root could be accessed with http://server:7777/folder but now using fusion middleware 11.1.1.5 Web Tier.. when I try to access any folders in my document root like http://server:7