TRUNC function for DATE

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:
user609308

it'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.

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 advance

    use this instead:
    TIMESTAMPADD(SQL_TSI_DAY, ( DAYOFMONTH(CURRENT_DATE) * -1) + 1, CURRENT_DATE)

  • Math functions for dates

    Hello,
    Have you ever used a math function for dates (MMDDYYYY) not just a number? I'm looking to send an email out based on date field. For example, if someone's contract expires on 1/1/2016, I'd like to send them out emails 30, 152, 212 and 335 days before that date. Right now, we have fields set up for those 30, 152, 212 and 335 days and would like to know if there's a math function that would automatically fill them in dynamically based on their contract exp. date.
    Thanks for your help and suggestions.

    rmabrey wrote:
    I have the calculator working but I am having trouble figuring out thow to do a square root function, nth factorial, absolute value, and Fibonacci. java.lang.Math.sqrt()
    nothing for factorial - write your own.
    java.lang.Math.abs()
    nothing for Fibonacci - write your own.
    %

  • Functions for date datatype

    Dear ALL...
    Someone know some function for query months, years, days from date columns....???
    Example for MSSQL...select datepart(date collumn) from table....select month(date column) from table....etc
    I´m studing Oracle alone to get a OCP (OCA first)......the bad thing...I Work with the MSSQL.. (to me, Oracle is the best RDBMS)...
    Thks
    Daniel

    Hi Daniel,
    Well I saw that you come from Brazil and as you are studying Oracle by yourself, take a look on my blog http://mportes.blogspot.com - There you're going to find a lot of technical stuff in Portuguese. For intance you might interest on this
    http://mportes.blogspot.com/2005/11/trabalhando-com-intervalos-de-datas.html
    About your question, you can start with
    SQL> select to_char(sysdate, 'Mon') from dual;
    TO_
    May
    1 row selected.
    SQL> select to_char(sysdate, 'Month') from dual;
    TO_CHAR(S
    May
    1 row selected.
    SQL> select to_char(sysdate, 'Month', 'nls_date_language=portuguese') from dual;
    TO_CHAR(S
    Maio
    1 row selected.To find more information, I suggest you stop by
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/functions180.htm#SQLRF06129
    and try out http://tahiti.oracle.com there you will find all docs from Oracle (free).
    And so on...

  • Function for dates

    Hi Experts!
    i want to calculate months b/w 2 dates , is there function for this . can i find days and year  also .plz tell me .
    thanks.

    HAI TRY THIS
    REPORT  ZSAN_SDN2.
    SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME.
    PARAMETER:
              N1 TYPE T,
              N2 TYPE T.
    DATA:     DN1 TYPE T,
              DN2 TYPE T,
              DN3 TYPE I,
              DN4 TYPE I,
              DN6 TYPE I,
              DN5 TYPE I.
    SELECTION-SCREEN:
    BEGIN OF LINE,
    PUSHBUTTON 2(10) BUT1 USER-COMMAND CALC,
    PUSHBUTTON 13(10) BUT2 USER-COMMAND EXIT,
    END OF LINE.
    SELECTION-SCREEN END OF BLOCK B1.
    AT SELECTION-SCREEN.
      CASE SY-UCOMM.
        WHEN 'CALC'.
          MOVE: N1 TO DN1,
                N2 TO DN2.
          DN3 = DN2 - DN1 .
          DN3 = DN3 MOD 86400.
          DN4 = DN3 / 3600 .
          DN6 = DN3 / 60 .
          DN5 = DN6 MOD 60.
          DN3 = DN3 MOD 3600 .
          DN3 = DN3 MOD 60 .
          LEAVE TO LIST-PROCESSING.
          SET PF-STATUS SPACE.
          WRITE: / DN4, 'hours'.
          WRITE: / DN5, 'minutes'.
          WRITE: / DN3, 'seconds'.
        WHEN 'EXIT'.
          LEAVE PROGRAM.
      ENDCASE.
    INITIALIZATION.
      BUT1 = 'calc'.
      BUT2 = 'exit'.

  • Between function for dates in Docmd.OpenReport rptName,,[date-filed] BETWEEN #date1# AND #date2# .. NOT WORKING

    Please assist, this is a nightmare indeed.
    Am trying to filter records in a report using the BETWEEN function, as below:
    strCondition = "Format([tblOrderLineTrack_main].[dtAllocation],'dd/MM/yyyy') BETWEEN Format(#06/02/2015#,'dd/MM/yyyy') AND Format(#07/03/2015#,'dd/MM/yyyy')" 
    DoCmd.OpenReport strDocName, acViewPreview, , strCondition, acWindowNormal
    The report is bringing up all records including those outside the specified range. What could be wrong with this???? Please assist, I am going nuts over this...

    The date literals I used are in the
    ISO standard for date notation of YYYY-MM-DD so are internationally unambiguous.  The only circumstance I can envisage in which it would not return the rows expected would be the extremely unlikely one where all the rows with dates in the range happen
    to have a date of 7 March 2015, and all have a non-zero time of day element.  There is no such thing in Access as a date value, only a date/time value so #2015-03-07# in fact represents the point of time at midnight at the start of 7 March 2015. 
    Any rows with date/time values of that date, but with a non-zero time of day would consequently fall outside the range.
    The above is so unlikely that I can't believe it to be the case, but the bullet-proof syntax would be:
        strCondition = "dtAllocation  >= #2015-02-06# " & _
              "AND dtAllocation < #2015-03-07#+1"
    i.e. all date/time values on or later than the start of the first day of the range (#2015-02-06 00:00:00#) and before the start of the day following the end of the range (#2015-03-08 00:00:00#)
    Assuming that the dtAllocation column is of date/time data type, and is returned as such in the report's RecordSource, not as a formatted string expression, I can see no reason on the information available why the above should not work, so if the report is
    still not returning any data some other reason not apparent to us must be suspected.  We are not in a position to debug the report, however, so what that might be I can't say.
    Ken Sheridan, Stafford, England

  • TO_CHAR Function for dates in UTF8 database

    Hi,
    Our database is UTF8 enabled infact we are using AL32UTF8 encoding. When we are running the following query then the results are displayed in Japanese instead of English for the 'DAY' and 'DY' clause.
    SELECT TO_CHAR(SYSDATE,'DAY') DAY,TO_CHAR(SYSDATE,'YEAR') YR,TO_CHAR(SYSDATE,'DY') DY,TO_CHAR(SYSDATE,'MON') MON FROM DUAL;
    DAY YEAR DY MON
    &#28779;&#26332;&#26085; TWO THOUSAND SIX &#28779; JAN
    Can anyone please tell me what is the reason behind this. Our other nls parameters are like;-
    SQL> sho parameter nls
    NAME TYPE VALUE
    nls_calendar string
    nls_comp string
    nls_currency string
    nls_date_format string
    nls_date_language string
    nls_dual_currency string
    nls_iso_currency string
    nls_language string AMERICAN
    nls_length_semantics string BYTE
    nls_nchar_conv_excp string FALSE
    nls_numeric_characters string
    nls_sort string
    nls_territory string AMERICA
    nls_time_format string
    nls_time_tz_format string
    nls_timestamp_format string
    nls_timestamp_tz_format string
    Thanks & Regards,
    Sudipta

    The date literals I used are in the
    ISO standard for date notation of YYYY-MM-DD so are internationally unambiguous.  The only circumstance I can envisage in which it would not return the rows expected would be the extremely unlikely one where all the rows with dates in the range happen
    to have a date of 7 March 2015, and all have a non-zero time of day element.  There is no such thing in Access as a date value, only a date/time value so #2015-03-07# in fact represents the point of time at midnight at the start of 7 March 2015. 
    Any rows with date/time values of that date, but with a non-zero time of day would consequently fall outside the range.
    The above is so unlikely that I can't believe it to be the case, but the bullet-proof syntax would be:
        strCondition = "dtAllocation  >= #2015-02-06# " & _
              "AND dtAllocation < #2015-03-07#+1"
    i.e. all date/time values on or later than the start of the first day of the range (#2015-02-06 00:00:00#) and before the start of the day following the end of the range (#2015-03-08 00:00:00#)
    Assuming that the dtAllocation column is of date/time data type, and is returned as such in the report's RecordSource, not as a formatted string expression, I can see no reason on the information available why the above should not work, so if the report is
    still not returning any data some other reason not apparent to us must be suspected.  We are not in a position to debug the report, however, so what that might be I can't say.
    Ken Sheridan, Stafford, England

  • How to use min-max functions for date in transformation in bpel?

    Hi,
    I have a requirement where i need to map earliest date of a Source field among all the dates to a target field in a transformation file in BPEL. There are functions called max-value-among-nodeset and min-value-among-nodeset in mathematical functions but they can be used only for numbers and strings.How to do these with dates?
    Thanks,
    Prasanna

    I exactly have the same requirement.Are you able to solve this?
    Its very urgent.Please help me.

  • How to use function for 'date' - 30 mins?

    Hi,
    I need to subtract 30 mins from date-time and display value in report?
    Any function?
    Thanks!
    Edited by: user12427117 on Mar 11, 2011 10:20 AM

    Date should be in canonical format
    i.e. YYYY-MM-DDThh:mm:ss+HH:MM
    or atleast YYYY-MM-DD
    when you want to divide you should have it as '1 div 48' and not 1/48
    Havent tried this one. So cant confirm if it works for mins

  • Function for Date Validation

    Hi,
    Can anyone tell me a Function Module to Valdate Date?
    Thanks & Regards
    Sheela

    Thanks for the Quick response.
    But i want a FM which will validate Every Date entered(Date format)
    Thanks Once again
    Regards
    Sheela

  • User response function() for Date Prompt

    Hi Experts,
    I have the requirement to show the date enterted in the prompt in the column of the webi report.
    I used the User response function e.g. =(UserResponse("Report Date").
    Now I am getting the value as 5/14/2014 12:00:00 AM. I dont want to see the time stamp with the date and aslo i want to change the format of the
    date to - yymmdd like 140514.
    Please provide your inputs.
    Thanks,
    Abhimanyu Sharma.

    Hi Arjit,
    I closed the thread.
    Can you please help in the below issue :
    I have two columns in the BEX report:
    1. "Local Count" which is restricted with Multiple Single Value variable "ZIN_CITY" on navigational Attribue 0ROUTE__0ROUTEID.
    2. "Out Count " which is restricted with Multiple Single Value variable "ZOUT_CITY" on navigational Attribue 0ROUTE__0ROUTEID.
    I put value 100 in prompt ZIN_CITY and 200 in ZOUT_CITY when i run this query in BOBJ WEbi.
    But i am getting the error - Not able to retrieve data. i checked in the backend and data is present and also Bex query is working fine.
    IF i change the property of variable to Single value then it is working fine in WEBi.
    Also i tried another method - irresprctive of setting the prompt in BEx on Nav attr 0ROUTE__0ROUTEID, i created the prompt in BOBJ WEBi, but LOVs (list of values)are not coming in the prompt.
    Please suggest on this?

  • Trunc function with date

    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
    thanks

    Hello,
    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

  • Is there any in-built function for date compare?

    I need function which compare dates?

    Hi,
    You can simply use > , <,etc.,
    Check this sample code.
    data : d1 type RM06B-EEIND ,
           d2 type sy-datum.
    d1 = sy-datum.
           CALL FUNCTION 'DATE_IN_FUTURE'
             EXPORTING
               anzahl_tage                   = '-7'
               import_datum                  =  d1
            IMPORTING
              EXPORT_DATUM_EXT_FORMAT       =  d2
              EXPORT_DATUM_INT_FORMAT       =  d2
    if d1 < d2.
    write 'h'.
    endif.

  • NWEngine function for date

    Is there a list of nwengine functions anywhere?  I take it to be a VBscript capable, but there's no guide on how to get it to do simple things like date and time in any of the sample scripts.  It is unable to resolve the %date% and %time% variables,
    NwEngine.Variables.ResolveString("%date%")
    yet it resolves other variable successfully,
    NwEngine.Variables.ResolveString("%windir%\saplogon.ini")
    NwEngine.Variables.ResolveString("%username%")
    What gives?

    Dim MyTime
    MyTime = CStr(Time)   ' Return current system time.
    Dim MyDate
    MyDate = CStr(Date)   ' MyDate contains the current system date.

  • Pivot table in BI Publisher: Different aggregate functions in data columns

    Hi, everyone!
    I`ve got some troubles with pivot table in my rtf-template.
    Here is my xml:
    <ROWSET>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000003</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>0.0</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>8</_BI_CALENDAR_._MONTH__>
         </ROW>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000004</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>165.58</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>7</_BI_CALENDAR_._MONTH__>
         </ROW>
         <ROW>
              <_BI_SUBRF_MO_._MUN_NAME_>МО Петроградский р-н</_BI_SUBRF_MO_._MUN_NAME_>
              <_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>714000004</_BI_PERS_ACCOUNT_CARD_._BI_PAC_NMB_>
              <_BI_MONTH_DEBET_._BI_DEBET_SUM_>165.58</_BI_MONTH_DEBET_._BI_DEBET_SUM_>
              <_BI_CALENDAR_._YEAR__>2009</_BI_CALENDAR_._YEAR__>
              <_BI_CALENDAR_._MONTH__>7</_BI_CALENDAR_._MONTH__>
         </ROW>
    ...... and so on..
    </ROWSET>
    In the pivot table i`d like to see one row for every BISUBRF_MO_._MUN_NAME_ using BICALENDAR_._YEAR__ and BICALENDAR_._MONTH__ as measures and sum of BIMONTH_DEBET_._BI_DEBET_SUM_ and count of unique (or at least just count of) BIPERS_ACCOUNT_CARD_._BI_PAC_NMB_ in cells.
    I create a pivot table using wizard, everything ok except one thing: as i understand, in pivot table for data in rows we can use only one kind of aggregate function e.g. only sum or only count.
    Is there any way to use different aggregate functions in data cells?
    Thank you

    As you can see, when we use crosstab tag we can specify only one aggregate function for data cells.
    Is there any way to get in XSL-FO (where we can what really happens) instead of:
    <T1>
    <xsl:value-of select="sum(current-group()/_BI_PERS_ACCOUNT_CARD_._BI_PAC_UNID_)"/>
    </T1>
    <T2>
    <xsl:value-of select="sum(current-group()/_BI_MONTH_DEBET_._BI_DEBET_SUM_)"/>
    </T2>
    this
    <T1>
    <xsl:value-of select="sum(current-group()/_BI_PERS_ACCOUNT_CARD_._BI_PAC_UNID_)"/>
    </T1>
    <T2>
    <xsl:value-of select="count(current-group()/_BI_MONTH_DEBET_._BI_DEBET_SUM_)"/>
    </T2>
    Edited by: user12115038 on 26.10.2009 8:08

Maybe you are looking for