TO_char ------------- To_date

Asalam and hello every one...
i am having a bit concept problem.. i have a condition that
my created_date should be greather then and equal to 26-nov-2009 and it should return 605 records
now when i am doing it with following method its returning 31 rows
where to_char(a.created, 'dd-mon-rr') >= '26-nov-09'
but if i do it with this method its working perfectly fine..
where to_date(a.created, 'dd-mon-rr') >= '26-nov-09'
now i want to know why its not returning all records when i am using to_char function

Because you're comparing apples with bananas.
Let's assume your CREATED column is of DATE datatype (you didn't mention that at all here).
Then your first predicate would be:
where to_char(a.created, 'dd-mon-rr') >= to_char(to_date('26-nov-09','dd-mon-rr'), 'dd-mon-rr')And the second one:
where  a.created >= to_date('26-nov-09', 'dd-mon-rr')Always compare DATES to DATES
Always compare STRINGS to STRINGS
Never compare STRINGS to DATES etc..
If your CREATED column is not of DATE datatype, then you need to adjust your design, change the table.
I wonder why, in the year 2010, you still use only 2 digits for the year part, instead of 4?

Similar Messages

  • How to use functions(to_char,to_date,trim..etc) in oracle xml table

    Hi I am new to oracle xml.
          In the below scenario how can use the functions like (to_char,To_date,trim..etc) In oracle xmltable."username","password" are inputs to my procedure.
    SELECT *
        INTO l_username,
                  l_password
        FROM xmltable('/InputParameters/ParamSet'
                        passing l_xmldoc
                        COLUMNS
                                l_username  VARCHAR2(28)    path 'username',
                                l_password VARCHAR2(28)     path 'password'
    and please Help how to write above with xmlquery.
    Thanks,

    In the same place you would for any regular SELECT statement
    SELECT TO_CHAR(...),
           TO_NUMBER(...)
      FROM random_table
    WHERE TO_CHAR(...) = col1
       AND TO_NUMBER(...) = col2
    username and password are the results of the SQL statement.  I'm guessing l_xmldoc is the input? There is not need to write something so simple as XQuery so what is your larger goal?  If you provide a clearer example and question, we can help you better.

  • Explain TO_CHAR(),TO_DATE(),TO_NUMBER() with examples

    Hi All,
    I have little bit confusion on TO_CHAR(),TO_DATE(),TO_NUMBER().
    Can any one please explain when will use that functions with examples.
    Thanks & Regards,

    974825 wrote:
    Hi All,
    I have little bit confusion on TO_CHAR(),TO_DATE(),TO_NUMBER().
    Can any one please explain when will use that functions with examples.
    Thanks & Regards,
    For to_date and to_char (as applied to dates) see: http://edstevensdba.wordpress.com/2011/04/07/nls_date_format/ - But I want to store my date as ...
    Once you understand how it works for dates, you should have a good foundation for understanding how it works for numbers.

  • To_Date function in the Where Clause

    Hello All,
    I'm having an issue using the to_date function that has me quite perplexed.
    I have two varchar2 fields, one with a date value in the format Mon, DD YYYY, the other has a time value in the format HH:MI PM.
    When I run my query one of the columns I retrieve looks like this TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM'). The two fields are concatenated together and converted to a date. This works fine.
    My problem occurs when I attempt to apply the same logic to the where clause of the aforementioned query. e.g. when I add the following criteria to my query and TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM') <= sysdate I get an ORA-01843: not a valid month error.
    To further illustrate my problem here are the two queries:
    Select d4.adate, e4.atime, TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM')
    from ....
    where ....
    The above query works.
    Select d4.adate, e4.atime, TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM')
    from ....
    where ....
    and TO_DATE (d4.adate || e4.atime, 'Mon DD, YYYYHH:MI PM') <= sysdate
    The second query does not work.
    The tables used and the limiting criteria are identical, except for the last one.
    Does anyone have any ideas why this could be happening.
    er

    Hello,
    Check this out. It does work. Do cut n paste sample
    data from your tables.
    SQL> desc test
    Name Null? Type
    ID NUMBER
    DDATE VARCHAR2(20)
    DTIME VARCHAR2(20)
    SQL> select * from test;
    ID DDATE DTIME
    1 Jan, 10 2006 12:32 PM
    2 Mar, 11 2005 07:10 AM
    3 Apr, 13 2006 03:12 AM
    4 Nov, 15 2003 11:22 PM
    5 Dec, 20 2005 09:12 AM
    6 Oct, 30 2006 10:00 AM
    7 Jan, 10 2006 12:32 PM
    8 Apr, 11 2005 07:10 AM
    9 May, 13 2006 03:12 AM
    10 Sep, 15 2003 11:22 PM
    11 Oct, 20 2005 09:12 AM
    12 Dec, 30 2006 10:00 AM
    12 rows selected.
    SQL> select id, ddate, dtime,
    2 to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM') AA,
    A,
    3 to_char(to_date(ddate||dtime,'Mon, DD YYYYHH:MI
    MI PM'),'Mon, DD YYYYHH:MI PM') BB
    4 from test;
    ID DDATE DTIME
    DTIME AA BB
    1 Jan, 10 2006 12:32 PM
    12:32 PM 10-JAN-06 Jan, 10 200612:32 PM
    2 Mar, 11 2005 07:10 AM
    07:10 AM 11-MAR-05 Mar, 11 200507:10 AM
    3 Apr, 13 2006 03:12 AM
    03:12 AM 13-APR-06 Apr, 13 200603:12 AM
    4 Nov, 15 2003 11:22 PM
    11:22 PM 15-NOV-03 Nov, 15 200311:22 PM
    5 Dec, 20 2005 09:12 AM
    09:12 AM 20-DEC-05 Dec, 20 200509:12 AM
    6 Oct, 30 2006 10:00 AM
    10:00 AM 30-OCT-06 Oct, 30 200610:00 AM
    7 Jan, 10 2006 12:32 PM
    12:32 PM 10-JAN-06 Jan, 10 200612:32 PM
    8 Apr, 11 2005 07:10 AM
    07:10 AM 11-APR-05 Apr, 11 200507:10 AM
    9 May, 13 2006 03:12 AM
    03:12 AM 13-MAY-06 May, 13 200603:12 AM
    10 Sep, 15 2003 11:22 PM
    11:22 PM 15-SEP-03 Sep, 15 200311:22 PM
    11 Oct, 20 2005 09:12 AM
    09:12 AM 20-OCT-05 Oct, 20 200509:12 AM
    12 Dec, 30 2006 10:00 AM
    10:00 AM 30-DEC-06 Dec, 30 200610:00 AM
    12 rows selected.
    SQL> select id, ddate, dtime,
    to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
    2 from test
    3 where id > 3
    4 and to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
    ') <= trunc(sysdate);
    ID DDATE DTIME
    DTIME TO_DATE(D
    4 Nov, 15 2003 11:22 PM
    11:22 PM 15-NOV-03
    5 Dec, 20 2005 09:12 AM
    09:12 AM 20-DEC-05
    7 Jan, 10 2006 12:32 PM
    12:32 PM 10-JAN-06
    8 Apr, 11 2005 07:10 AM
    07:10 AM 11-APR-05
    10 Sep, 15 2003 11:22 PM
    11:22 PM 15-SEP-03
    11 Oct, 20 2005 09:12 AM
    09:12 AM 20-OCT-05
    6 rows selected.
    SQL> select id, ddate, dtime,
    to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
    2 from test
    3 where id > 3
    4 and to_date(ddate||dtime,'Mon, DD YYYYHH:MI PM')
    ') <= sysdate;
    ID DDATE DTIME
    DTIME TO_DATE(D
    4 Nov, 15 2003 11:22 PM
    11:22 PM 15-NOV-03
    5 Dec, 20 2005 09:12 AM
    09:12 AM 20-DEC-05
    7 Jan, 10 2006 12:32 PM
    12:32 PM 10-JAN-06
    8 Apr, 11 2005 07:10 AM
    07:10 AM 11-APR-05
    10 Sep, 15 2003 11:22 PM
    11:22 PM 15-SEP-03
    11 Oct, 20 2005 09:12 AM
    09:12 AM 20-OCT-05
    6 rows selected.
    -SriSorry Sri, but I fail to see what you mean. How is what you're doing any different than what I'm doing?

  • Problem in to_char function

    hi,
    when i am using select to_char(sysdate,'dd.mm.yy') from dual* then its giving the right answer 27.09.10.
    but when i am replacing the sysdate with the particular date format of oracle that is select to_char('27-Sep-10','dd.mm.yy') from dual* then its throwing error.
    My question is that dd-Mon-yy* is the default oracle date format. then if i replace sysdate+ by that format then why the error is thrown???
    please help me out.
    thanks.

    user12780416 wrote:
    hi,
    when i am using select to_char(sysdate,'dd.mm.yy') from dual* then its giving the right answer 27.09.10.
    but when i am replacing the sysdate with the particular date format of oracle that is select to_char('27-Sep-10','dd.mm.yy') from dual* then its throwing error.
    My question is that dd-Mon-yy* is the default oracle date format. then if i replace sysdate+ by that format then why the error is thrown???
    please help me out.
    thanks.Because you are asking to to_char a character.
    If you want to get a specific format out of an existing DATE data type (for display purposes) you would need to have a DATE to begin with.
    TUBBY_TUBBZ?select to_char('27-Sep-10','dd.mm.yy') from dual;
    select to_char('27-Sep-10','dd.mm.yy') from dual
    ERROR at line 1:
    ORA-01722: invalid number
    Elapsed: 00:00:00.81
    TUBBY_TUBBZ?select to_char(to_date('27-Sep-10', 'dd-mon-yy') ,'dd.mm.yy') from dual;
    TO_CHAR(TO_DATE('27-SEP-
    27.09.10
    1 row selected.
    Elapsed: 00:00:00.85
    TUBBY_TUBBZ?In your case.

  • Orcl:query-database gives error when using to_char function in select stmt

    hi
    Use Case : We get a csv file ("bank_import_<MMDDYYYYY>.csv") from the bank containing the transactions occured for the month. The date in the filename is retrieved into string and i need to convert this string to the format "MON-DD-YYYY". This is the required format for an header table which takes this string as primary key.
    Code:
    statement_name = '11302206'.........
    <copy>
    <from expression="concat("'select to_char(to_date('",bpws:getVariableData('statement_name') ,"','MMDDYYYY'),'MON-DD-YYYY') from dual'")"/>
    <to variable="xpath"/>
    </copy>
    <copy>
    <from expression="orcl:query-database(bpws:getVariableData('xpath'),false(),false(),'jdbc:oracle:thin:apps/apps@croaker:1529:RSICMI')"/>
    <to variable="statement_name"/>
    </copy>
    Error:
    [2006/12/06 19:13:04] Updated variable "xpath" less
    <xpath>'select to_char(to_date('10302006','MMDDYYYY'),'MON-DD-YYYY') from dual'</xpath>
    [2006/12/06 19:13:04] "XPathException" has been thrown. less
    XPath expression failed to execute.
    Error while processing xpath expression, the expression is "orcl:query-database(bpws:getVariableData("xpath"), false(), false(), "jdbc:oracle:thin:apps/apps@croaker:1529:RSICMI")", the reason is .
    Please verify the xpath query.
    Log Message:
    <2006-12-06 19:13:04,595> <DEBUG> <UAT.collaxa.cube.xml> <XPathUtil::evaluate> XPathQuery[concat("'select to_char(to_date('", bpws:getVariableData("statement_name"), "','MMDDYYYY'),'MON-DD-YYYY') from dual'")], XPath Result: class=java.lang.String value='select to_char(to_date('10302006','MMDDYYYY'),'MON-DD-YYYY') from dual'
    <2006-12-06 19:13:04,595> <DEBUG> <UAT.collaxa.cube.xml> <XPathUtil::initXPath> namespaceMapping is: rootMap: {bpws=http://schemas.xmlsoap.org/ws/2003/03/business-process/, xp20=http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20, ns4=http://xmlns.oracle.com/pcbpel/adapter/db/top/BAIBankUpload, ldap=http://schemas.oracle.com/xpath/extension/ldap, xsd=http://www.w3.org/2001/XMLSchema, ns5=http://xmlns.oracle.com/pcbpel/adapter/file/, client=http://xmlns.oracle.com/BAI_BankUpload, ora=http://schemas.oracle.com/xpath/extension, ns1=http://xmlns.oracle.com/pcbpel/adapter/file/readBAIBankImportCSV/, ns3=http://TargetNamespace.com/readBAIBankImportCSV, ns2=http://xmlns.oracle.com/pcbpel/adapter/db/Insert_SI_CE_STATEMENT_LINES_INT/, bpelx=http://schemas.oracle.com/bpel/extension, orcl=http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc, =http://schemas.xmlsoap.org/ws/2003/03/business-process/}
    scopedMap: {}
    <2006-12-06 19:13:04,751> <DEBUG> <UAT.collaxa.cube.xml> <XPathUtil::evaluate> XPathQuery :orcl:query-database(bpws:getVariableData("xpath"), false(), false(), "jdbc:oracle:thin:apps/apps@croakercom:1529:RSICMI")
    org.collaxa.thirdparty.jaxen.FunctionCallException
         at org.collaxa.thirdparty.jaxen.FunctionCallException.fillInStackTrace(FunctionCallException.java:124)
         at java.lang.Throwable.<init>(Throwable.java:195)
         at java.lang.Exception.<init>(Exception.java:41)
         at org.collaxa.thirdparty.jaxen.saxpath.SAXPathException.<init>(SAXPathException.java:83)
         at org.collaxa.thirdparty.jaxen.JaxenException.<init>(JaxenException.java:82)
         at org.collaxa.thirdparty.jaxen.FunctionCallException.<init>(FunctionCallException.java:86)
         at oracle.tip.pc.services.functions.ExtFuncFunction$QueryDatabaseFunction.call(ExtFuncFunction.java:190)
         at org.collaxa.thirdparty.jaxen.expr.DefaultFunctionCallExpr.evaluate(DefaultFunctionCallExpr.java:184)
         at org.collaxa.thirdparty.jaxen.expr.DefaultXPathExpr.asList(DefaultXPathExpr.java:107)
         at org.collaxa.thirdparty.jaxen.BaseXPath.selectNodesForContext(BaseXPath.java:724)
         at org.collaxa.thirdparty.jaxen.BaseXPath.selectNodes(BaseXPath.java:253)
         at org.collaxa.thirdparty.jaxen.BaseXPath.evaluate(BaseXPath.java:210)
         at com.collaxa.cube.xml.xpath.XPathUtil.evaluate(XPathUtil.java:93)
         at com.collaxa.cube.engine.ext.wmp.BPELAssignWMP.evalFromValue(BPELAssignWMP.java:501)
         at com.collaxa.cube.engine.ext.wmp.BPELAssignWMP.__executeStatements(BPELAssignWMP.java:122)
         at com.collaxa.cube.engine.ext.wmp.BPELActivityWMP.perform(BPELActivityWMP.java:188)
         at com.collaxa.cube.engine.CubeEngine.performActivity(CubeEngine.java:3408)
         at com.collaxa.cube.engine.CubeEngine.handleWorkItem(CubeEngine.java:1836)
         at com.collaxa.cube.engine.dispatch.message.instance.PerformMessageHandler.handleLocal(PerformMessageHandler.java:75)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.handleLocalMessage(DispatchHelper.java:166)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.sendMemory(DispatchHelper.java:252)
         at com.collaxa.cube.engine.CubeEngine.endRequest(CubeEngine.java:5438)
         at com.collaxa.cube.engine.CubeEngine.createAndInvoke(CubeEngine.java:1217)
         at com.collaxa.cube.engine.delivery.DeliveryService.handleInvoke(DeliveryService.java:511)
         at com.collaxa.cube.engine.ejb.impl.CubeDeliveryBean.handleInvoke(CubeDeliveryBean.java:335)
         at ICubeDeliveryLocalBean_StatelessSessionBeanWrapper16.handleInvoke(ICubeDeliveryLocalBean_StatelessSessionBeanWrapper16.java:1796)
         at com.collaxa.cube.engine.dispatch.message.invoke.InvokeInstanceMessageHandler.handle(InvokeInstanceMessageHandler.java:37)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.handleMessage(DispatchHelper.java:125)
         at com.collaxa.cube.engine.dispatch.BaseScheduledWorker.process(BaseScheduledWorker.java:70)
         at com.collaxa.cube.engine.ejb.impl.WorkerBean.onMessage(WorkerBean.java:86)
         at com.evermind.server.ejb.MessageDrivenBeanInvocation.run(MessageDrivenBeanInvocation.java:123)
         at com.evermind.server.ejb.MessageDrivenHome.onMessage(MessageDrivenHome.java:755)
         at com.evermind.server.ejb.MessageDrivenHome.run(MessageDrivenHome.java:928)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:186)
         at java.lang.Thread.run(Thread.java:534)
    Root cause:
    java.lang.ClassCastException
         at oracle.tip.pc.services.functions.ExtFuncFunction$QueryDatabaseFunction.call(ExtFuncFunction.java:158)
         at org.collaxa.thirdparty.jaxen.expr.DefaultFunctionCallExpr.evaluate(DefaultFunctionCallExpr.java:184)
         at org.collaxa.thirdparty.jaxen.expr.DefaultXPathExpr.asList(DefaultXPathExpr.java:107)
         at org.collaxa.thirdparty.jaxen.BaseXPath.selectNodesForContext(BaseXPath.java:724)
         at org.collaxa.thirdparty.jaxen.BaseXPath.selectNodes(BaseXPath.java:253)
         at org.collaxa.thirdparty.jaxen.BaseXPath.evaluate(BaseXPath.java:210)
         at com.collaxa.cube.xml.xpath.XPathUtil.evaluate(XPathUtil.java:93)
         at com.collaxa.cube.engine.ext.wmp.BPELAssignWMP.evalFromValue(BPELAssignWMP.java:501)
         at com.collaxa.cube.engine.ext.wmp.BPELAssignWMP.__executeStatements(BPELAssignWMP.java:122)
         at com.collaxa.cube.engine.ext.wmp.BPELActivityWMP.perform(BPELActivityWMP.java:188)
         at com.collaxa.cube.engine.CubeEngine.performActivity(CubeEngine.java:3408)
         at com.collaxa.cube.engine.CubeEngine.handleWorkItem(CubeEngine.java:1836)
         at com.collaxa.cube.engine.dispatch.message.instance.PerformMessageHandler.handleLocal(PerformMessageHandler.java:75)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.handleLocalMessage(DispatchHelper.java:166)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.sendMemory(DispatchHelper.java:252)
         at com.collaxa.cube.engine.CubeEngine.endRequest(CubeEngine.java:5438)
         at com.collaxa.cube.engine.CubeEngine.createAndInvoke(CubeEngine.java:1217)
         at com.collaxa.cube.engine.delivery.DeliveryService.handleInvoke(DeliveryService.java:511)
         at com.collaxa.cube.engine.ejb.impl.CubeDeliveryBean.handleInvoke(CubeDeliveryBean.java:335)
         at ICubeDeliveryLocalBean_StatelessSessionBeanWrapper16.handleInvoke(ICubeDeliveryLocalBean_StatelessSessionBeanWrapper16.java:1796)
         at com.collaxa.cube.engine.dispatch.message.invoke.InvokeInstanceMessageHandler.handle(InvokeInstanceMessageHandler.java:37)
         at com.collaxa.cube.engine.dispatch.DispatchHelper.handleMessage(DispatchHelper.java:125)
         at com.collaxa.cube.engine.dispatch.BaseScheduledWorker.process(BaseScheduledWorker.java:70)
         at com.collaxa.cube.engine.ejb.impl.WorkerBean.onMessage(WorkerBean.java:86)
         at com.evermind.server.ejb.MessageDrivenBeanInvocation.run(MessageDrivenBeanInvocation.java:123)
         at com.evermind.server.ejb.MessageDrivenHome.onMessage(MessageDrivenHome.java:755)
         at com.evermind.server.ejb.MessageDrivenHome.run(MessageDrivenHome.java:928)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:186)
         at java.lang.Thread.run(Thread.java:534)
    <2006-12-06 19:13:04,751> <ERROR> <UAT.collaxa.cube.xml> ORABPEL-09500
    XPath expression failed to execute.
    Error while processing xpath expression, the expression is "orcl:query-database(bpws:getVariableData("xpath"), false(), false(), "jdbc:oracle:thin:apps/apps@croaker:1529:RSICMI")", the reason is .
    Please verify the xpath query.

    Hi,
    QAbdul wrote:
    when I tried to execute the followingin XMLQuery by calling TO_CHAR() whithin this query I am getting this error"ORA-19237: XP0017 - unable to resolve call to function - fn:TO_CHARTO_CHAR is a SQL function, XQuery is unaware of it.
    XPath 2.0 specifications define a fn:format-date function but Oracle has not included yet in its XQuery implementation.
    Easiest way to go is A_Non's solution, but if you need to format at multiple places in the query, you can declare a local XQuery function.
    For example, to format to "DD/MM/YYYY" from the canonical xs:date format "YYYY-MM-DD" :
    {code}
    declare function local:format-date($d as xs:date) as xs:string
    let $s := xs:string($d)
    return concat(
    substring($s, 10, 2), "/",
    substring($s, 7, 2), "/",
    substring($s, 2, 4)
    {code}
    and an example of use :
    {code}
    SQL> CREATE TABLE test_xqdate AS SELECT sysdate dt FROM dual;
    Table created
    SQL> SELECT *
    2 FROM XMLTable(
    3 'declare function local:format-date($d as xs:date) as xs:string
    4 {
    5 let $s := xs:string($d)
    6 return concat(
    7 substring($s, 10, 2), "/",
    8 substring($s, 7, 2), "/",
    9 substring($s, 2, 4)
    10 )
    11 }; (: :)
    12 for $i in ora:view("TEST_XQDATE")/ROW/DT
    13 return element e {
    14 attribute xs_date_format { $i/text() },
    15 attribute local_format { local:format-date($i) }
    16 }'
    17 COLUMNS
    18 xs_date_format VARCHAR2(10) PATH '@xs_date_format',
    19 local_format VARCHAR2(10) PATH '@local_format'
    20 )
    21 ;
    XS_DATE_FORMAT LOCAL_FORMAT
    2010-10-28 28/10/2010
    {code}

  • Error in to_date

    Hi,
    I am getting an error on the below query, in the "to_date" function part.
    select *
      from rf_bankaccount_avg t
    where t.month_year between
           to_char(to_date((SELECT ADD_MONTHS(LAST_DAY(SYSDATE), -7) + 1 AS FROM dual),'mm-yyyy')) and
           to_char(to_date((select last_day(sysdate - to_char(sysdate, 'DD')) from dual),'mm-yyyy'))
       and t.account_type = 1
       and t.addnl_factype_no = '0'
       and t.ret_factype = 'A'
       and t.company_bankcode = '';i am getting the error "ORA-01841: (full) year must be between -4713 and +9999, and not be 0"
    Edited by: 872435 on Apr 2, 2012 2:25 AM

    Hi,
    You can see many mistakes more clearly if you format your code.
    The part that is apparantly raising this error is:
    to_char ( to_date ( ( SELECT  ADD_MONTHS ( LAST_DAY (SYSDATE)
                                            , -7
                              ) + 1     AS
                    FROM    dual
                , 'mm-yyyy'
         ) There's a mistake around the keyword "AS". Either you meant to have a column alias after it, or you didn't mean to have the keyword "AS" at all.
    You can see other mistakes, here, too. For example, you're calling TO_DATE on something that is already a DATE, and you're calling TO_CHAR with only 1 argument.
    Whatever you're trying to do, this is a very awkward way to do it.
    What are you trying to do?
    Are you trying to see if t.month_year is in the 6 months ending with last month? (That is, when the query is run any time in April, 2012, do you want to find rows where t.month_year is between October, 2011 and March, 2012, inclusive?) Assuming t.month_year is a DATE, that's just
    WHERE   t.month_year >= ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), -6)
    AND     t.month_year <              TRUNC (SYSDATE, 'MONTH')This assumes that t.month_year is a DATE. If it's some other data type, the solution is a little more complicated. That's to be expected; storing date information in any data type other than DATE makes things more complicated.

  • ORA-01722: invalid number while using TO_CHAR function.

    Hi All,
    I was using this query from around past 1 year and everything was working fine.
    select to_char ( '2012/12/23','YYYY/MM/DD') from dual;
    how ever from monday onwards , this is returning me with the following error.
    ORA-01722: invalid number
    The NLS_DATE_FORMAT is DD-MON-RR in my oracle 11g version.
    Please help me regarding this.

    Your date is in literal & your intended format so no need to format it again.
    select to_char ( '2012/12/23','YYYY/MM/DD') from dual;
    same as
    select '2012/12/23' from dual;I don't know your need so same code to work!!!
    select to_char(to_date('2012/12/23','YYYY/MM/DD'),'DD/MM/YYYY') from dualsunnymoon wrote:
    where vale for ? is coming as '2011/12/23' from java.
    the whole application has to be changed and retested.
    >
    It it comes as a text convert it to date TO_DATE('2011/12/23','YYYY/MM/DD') to use in comparison operators like =, != , > , < etc. Comparison operators used taking date as literal will give you wrong result.
    Better to do it right even it takes huge effort.
    Edited by: Lokanath Giri on ३ जनवरी, २०१२ १२:०६ अपराह्न

  • Rounding minutes up or down when using a to_char function

    Hi, this is my query: select (to_char(incident_date, 'HH24:MI')) Time from cla_event
    Incident_Date column has a date and time in it. The above query pulls the time which is what I require. I do however want the hour to be rounded down if less than 30min and up if grater than 30min. For example 21:29 becomes 21:00 and 21:37 becomes 22:00.
    This is just a single column in my query so I'm hoping for a simple solution if there is one.
    Thanks!!!
    Banner:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE     11.2.0.2.0     Production"
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    WITH
         t_data AS
              select '21:29' as col1 from dual union all
              select '21:32' from dual
    SELECT
         col1,
         CASE
              WHEN SUBSTR(col1,                      -2) >= 30
              THEN
                   TO_CHAR((to_date(col1, 'HH24:MI'))+1/24,'HH24')||':00'               
              ELSE
                   TO_CHAR((to_date(col1, 'HH24:MI')),'HH24')||':00'
         END new_val,
         TO_CHAR(ROUND((to_date(col1, 'HH24:MI')),'hh24'),'HH24:MI') as new_val2
    FROM
         t_data;
    COL1  NEW_VAL NEW_VAL2
    21:29 21:00   21:00   
    21:32 22:00   22:00 Edited by: bpat on Oct 20, 2011 11:49 AM
    Added Rafu's solution too

  • TO_DATE function issue in Oracle 10g R2

    Hi,
    This SQL statement works in Oracle 10g R2 env & Oracle 9i R2.
    SQL>select TO_CHAR(TO_DATE(SYSDATE,'DD/MM/RRRR','NLS_DATE_LANGUAGE = ''AMERICAN'''),'RRRR/MM/DD','NLS_DATE_LANGUAGE = ''AMERICAN''') SYSTEM_DATE
    from dual;
    But after changing the session to Arabic it does not work in Oracle 10g R2 env but works in Oracle 9i R2 env.
    SQL> alter session set nls_language='ARABIC';
    SQL>select TO_CHAR(TO_DATE(SYSDATE,'DD/MM/RRRR','NLS_DATE_LANGUAGE = ''AMERICAN'''),'RRRR/MM/DD','NLS_DATE_LANGUAGE = ''AMERICAN''') SYSTEM_DATE
    from dual;
    Some one please confirm this will not work in Oracle 10g R2 env after altering the session.
    Rgds,

    The error message is:
    ORA-01858: a non-numeric character was found where a numeric was expected.\
    Rgds.

  • Problem with to_date('date_char') in a where clause

    A little confused with this plsql error.
    This code works...
    select to_date(value,'dd/mm/yy') from kip_lov where domain='MODULE START1'
    If I try to put the "to_date(value,'dd/mm/yy')" expression in the where clause I get an invalid year error,
    select to_date(value,'dd/mm/yy') from kip_lov where domain='MODULE START1'
    AND to_date(value,'dd/mm/yy') = SYSDATE
    "Error report:
    SQL Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0"
    The variable "value" is a varchar2 in the format dd/mm/yy.
    Note: the following code also works fine,
    select to_date(value,'dd/mm/yy') - sysdate from kip_lov where domain='MODULE START1'
    I have modified my lov table so that it uses the date type now and it works fine. Just curious as to why the above example does not work.
    Sam.

    check your table description and values first, is there any dd/mm/yy or mm/dd/yy confliction
    A scenario which runs.
    SQL> desc trya
    Name Null? Type
    UPDATED_ON DATE
    UPD VARCHAR2(8)
    SQL> select * from trya
    2 ;
    UPDATED_O UPD
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    UPDATED_O UPD
    31-JUL-06 31/07/06
    31-JUL-06 31/07/06
    13 rows selected.
    SQL> SELECT * FROM trya WHERE TO_CHAR(TO_DATE(upd,'dd/mm/yy'))=SYSDATE;
    no rows selected
    SQL> SELECT * FROM trya WHERE TO_DATE(upd,'dd/mm/yy')=SYSDATE;
    no rows selected
    SQL>
    NO error on both syntax
    rgds,
    Rup

  • ORA-06502 use TO_NUMBER and TO_CHAR

    Hi,
    Oracle Database 10g 10.2.0.5.0.
    I have mistake in this code:
    nSuper_ NUMBER(6,2);
    dbms_output.put_line('Super: ' ||  TO_NUMBER(nSuper_,'FM9G990D90'));
    ...I should use "TO_CHAR"...but my test is not failure, why?:
    --a)
    SET SERVEROUTPUT ON
    DECLARE
      nSuper_ NUMBER(6,2);
    BEGIN
      nSuper_ := 666.12;
      dbms_output.put_line('Super: ' ||  TO_NUMBER(nSuper_,'FM9G990D90'));
    END;
    Super: 666,12
    --b)
    SET SERVEROUTPUT ON
    DECLARE
      nSuper_ NUMBER(6,2);
    BEGIN
      nSuper_ := 6666.12;
      dbms_output.put_line('Super: ' ||  TO_NUMBER(nSuper_,'FM9G990D90'));
    END;
    DECLARE
    ERROR en línea 1:
    ORA-06502: PL/SQL: error  numérico o de valor
    ORA-06512: en línea 5
    --c)
    SET SERVEROUTPUT ON
    DECLARE
      nSuper_ NUMBER(6,2);
    BEGIN
      nSuper_ := 6666.12;
      dbms_output.put_line('Super: ' ||  TO_CHAR(nSuper_,'FM9G990D90'));
    END;
    Super: 6.666,12Why "a" not fail and "b" if it fails?

    Hi,
    jortri wrote:
    Why "a" not fail and "b" if it fails?Because Oracle RDBMS is (too) kind to you for "simple" conversion like that (but that doesn't mean you won't get errors...).
    The to_number function takes as input <b>string</b>.
    You're sending to it a <b>number</b>, so Oracle does this : +"Ok this guy is sending a number into a TO_NUMBER function when it should be a string, let's do an implicit conversion for him transparently"+.
    Your to_number(my_number_variable,'FM9G990D90') actually becomes this :to_number( to_char(my_number_variable) ,'FM9G990D90')But, as you can notice, the to_char function is not given any format, so it uses your session default which could be way different from the mask you're providing to the to_number function.
    Please execute this to understand what your failing to_number function is receiving as input :select 6666.12 number_implicitly_converted from dual;By default, on my machine it displays this :Scott@my11g SQL>select 6666.12 number_implicitly_converted from dual;
    NUMBER_IMPLICITLY_CONVERTED
                        6666.12Now if I use it as input of your to_number function with your format mask :Scott@my11g SQL>select TO_NUMBER('6666.12','FM9G990D90') back_to_number from dual;
    select TO_NUMBER('6666.12','FM9G990D90') back_to_number from dual
    ERROR at line 1:
    ORA-01722: invalid numberTadammm !
    Conclusion : Don't mess up the use of to_number/to_char/to_date functions.
    :-)

  • To_date function working correctly?

    Hi Guys,
    Trying to get dates from the table where the date is less than 100 AD but its not working correctly. Could you please comment where Im going wrong?
    Thanks,
    St
    select plan_1_date from TR_PLANNED_EVENT where plan_1_date < to_date('100AD','YYYAD')

    Saurabh T wrote:
    You should compare similar date formats.Dates don't have formats.
    >
    Try below to make date formats similar:
    select plan_1_date from TR_PLANNED_EVENT where to_char(plan_1_date, 'DD-MON-YYYY') < to_char(to_date('100AD','YYYAD'), 'DD-MON-YYYY')
    This is called string comparison and is not comparing dates and will produce the wrong answer like this -
    SQL> with mytable as
      2    (
      3    select date '2010-01-28' + level mydate from dual
      4    connect by level <= 5
      5    )
      6  select mydate, sysdate from mytable;
    MYDATE     SYSDATE
    01/29/2010 07/13/2010
    01/30/2010 07/13/2010
    01/31/2010 07/13/2010
    02/01/2010 07/13/2010
    02/02/2010 07/13/2010
    SQL> edi
    Wrote file afiedt.sql
      1  with mytable as
      2    (
      3    select date '2010-01-28' + level mydate from dual
      4    connect by level <= 5
      5    )
      6  select mydate, sysdate from mytable
      7* where to_char(mydate, 'DD-MON-YYYY') < to_char(sysdate, 'DD-MON-YYYY')
    SQL> /
    MYDATE     SYSDATE
    02/01/2010 07/13/2010
    02/02/2010 07/13/2010
    Hope it helps.It is very unhelpful and incorrect.
    Edited by: 3360 on Jul 13, 2010 8:08 AM

  • To_date function from dual

    Hi All,
    i have issued SELECT TO_DATE('07:19:47', 'HH:MI:SS') FROM DUAL
    in toad, and got the below output.
    11/1/2007 7:19:47 AM
    Even sqlplus returns
    TO_DATE('
    01-NOV-07
    Can anyone explain why this wont show sysdate? thanks

    If I understand correctly, you're wondering why
    SELECT TO_DATE('07:19:47', 'HH:MI:SS') FROM DUALreturns a date of Nov. 1 instead of today (Nov. 2)
    Interesting - I get the same result on 10gR2. Selecting SYSDATE gives me back today's date. I tried Justin's suggestion to alter the NLS_DATE format (didn't see why RR would affect the day, but tried it anyway) but this did not affect the result.
    I think the default day the query is converting the date string to is somehow being evaluated one day behind, as sysdate
    sql>run
      1*  SELECT to_char(TO_DATE('07:19:47', 'HH:MI:SS'),'mm/dd/yyyy hh24:mi:ss') FROM DUAL
    TO_CHAR(TO_DATE('07
    11/01/2007 07:19:47.

  • To_date function treats 2010 as 0010

    Hi,
    I am trying to run the follwoing SQL.
    SELECT to_char(to_date(date1,'DD/MM/YYYY') from TABLE1
    i see resultset in which YEAR is pulled out as 0010 instead of 2010.
    however, if i run the following, i get YEAR in the resultset as 2010
    SELECT to_char(to_date(date1,'DD/MM/YY') from TABLE1
    date1 is of type DATE in the table.
    Any idea what happens? Beacuse of this my comparison operators fail .

    791184 wrote:
    Hi,
    I am trying to run the follwoing SQL.
    SELECT to_char(to_date(date1,'DD/MM/YYYY') from TABLE1
    i see resultset in which YEAR is pulled out as 0010 instead of 2010.
    however, if i run the following, i get YEAR in the resultset as 2010
    SELECT to_char(to_date(date1,'DD/MM/YY') from TABLE1
    date1 is of type DATE in the table.
    Any idea what happens? Beacuse of this my comparison operators fail .The first one:
    They are "pulled out" as 0010 because somebody had mistakenly pulled it in this way.
    SQL> create table t1 (date1 date);
    Table created.
    SQL> insert into t1 values (to_date('27/08/10', 'dd/mm/yy'));
    1 row created.
    SQL> insert into t1 values (to_date('27/08/10', 'dd/mm/yyyy'));
    1 row created.
    SQL> select * from t1;
    DATE1
    27/08/2010
    27/08/0010
    SQL>Can you see the second insert? The format mask doesn't match the string given.
    And your second one is not working. You didn't do a "copy - paste".
    SQL> SELECT to_char(to_date(date1,'DD/MM/YY') from t1;
    SELECT to_char(to_date(date1,'DD/MM/YY') from t1
    ERROR at line 1:
    ORA-00907: missing right parenthesis
    SQL> select to_char(date1, 'dd/mm/yyyy') from t1;
    TO_CHAR(DA
    27/08/2010
    27/08/0010
    SQL> select to_char(date1, 'dd/mm/yy') from t1;
    TO_CHAR(
    27/08/10
    27/08/10
    SQL> The date stored in Oracle is always the same, but what you get displayed is what you give in the format mask (dd/mmyy or dd/mm/yyyy)

Maybe you are looking for

  • How do I create an interactive map?

    How do I create an interactive map using digital publishing suite in InDesign? Basically it's an amusement park map and when you click on a button I want a description page of a ride to show that has interactivity on it and then it can be closed and

  • Property executeInBackground not found on flash.desktop.nativeExtensions on iOS

    Hi, I'm unable to set this property in my iOS app as per the documentation. I'm on AIR version 3.3.0.3650 but cannot read or write to this property (though it does appear in code completion within Flash Builder. How to I get access to it? Thanks

  • Whoohoow! Forum Preview at its best!

    Very clean, very nice. Nice options for paragraph & text... attaching files, list goes on. Hopefully and especially new users will behave and do not screw up the clean look with bunch of useless smilies, signatures and useles stuff. My Fear: If possi

  • Lockscreen instead of Music Cover

    hello there. I have just got a weird bug on my iPhone 4 and i got a issue about what happens when I'm listening to music and my iPhone is lock-screen mode (simply say it is locked). What is happening is that when I'm trying to check the song name and

  • W500 4061-W8Q. Vertical Lines - BSOD

    Hi! I have my W500 4061-W8Q for almost 3 years already and it has been working perfectly.... till now. I have changed nothing in my system, but today in the evening i got a BSOD with "ati2dvag" error and that device has gone into "infinite loop"... I