TO_DATE function's weird feature
Hi Friends,
Is the following statement workable?
select to_date('09,06,16 12:12:59','yyyy-mm-dd hh24@mi,ss') from dual;
My own anwser is YES but the result is decided by NLS_DATE_FORMAT.
Have you any different opinions?
Hi Dear Friends,
In my own experience, the result depends on nls parameter NLS_DATE_FORMAT and the second parameter in to_date(arg1,arg2) function. For example, if you update the nls parameter NLS_DATE_FORMAT to be "YYYY-MM-DD" and execute the following statement, what the result do you expect?
select to_date('09,06,16 12:12:59','yyyy/mm/dd hh24@mi,ss') from dual;and try the following one again:
select to_date('09.06.16 12:12:59','rrrr\mm\dd hh24@mi,ss') from dual;In my own understanding, the statement execute process is like this:
1. Execute to_date function and get the Date result. If to_date function does not provide the second argument, the nls parameter NLS_DATE_FORMAT will be used. It is the truth that the separator character does not matter in the date format.
2. Pass Date result to the tool such as sqlplus or sqldeveloper, to_char function is executed in background, the result will be printed to us. The format of to_char function is decided by nls parameter NLS_DATE_FORMAT. So, if you udpate nls parameter NLS_DATE_FORMAT to be 'rrrr\mm\dd hh24@mi,ss' in above examples, you will get the result with the same format - the separator is the same of that in nls parameter NLS_DATE_FORMAT. Just try it! :)
So, that's my own conclusion, if you have different views, please do not hesitate to share with me.
Edited by: wengm on Mar 14, 2011 8:06 PM
Similar Messages
-
Oracle date field not comparing properly with to_date function
Hi, i'm facing a very weird problem. I'm triyng to retrieve a range of records in between two dates. However, it doesn't seem to understand. What am i doing wrong?
let's say my table (MYRECORD) has this (where id is varchar and date is DATE and not timestamp) :
ID DATE
1 22-JAN-08
2 22-JAN-08
3 21-JAN-10
4 11-FEB-10
5 11-FEB-10
So, let's say iwanna get the records from 21st Jan 2010 :
select
date as ori_date from MYRECORD where
date = to_date('21/01/2010','DD/MM/YYYY')
1.)
it is not returning any record at all. I'm having problems with between dates and the to_date function as well,
so i'm trying to get the 'equal' part working first. I can't use to_char because at the end of the day,
i need to compare between dates.
what am i doing wrong? has this got anything to do with what kind of format is set up on the oracle server?
running :
SELECT value FROM v$nls_parameters WHERE parameter ='NLS_DATE_FORMAT'
returns DD-MON-RR
2.) how do we compare dates from the table according to our own specified format from our passed string? I only have READONLY access to this database
so i can't set anything up.please run this
select To_date(date, 'DD/MM/YYYY HH24:MI:SS') as ori_date from MYRECORDprobably you have hour minute second info in your table, to_date('01012010','DDMMYYYY') means hour, minute and second is 00:00:00 so it wont be equal.The database field type is actually 'DATE' and not datetime. I've also ran the sql you provided to show its timestamp, but it's not showing anything as far as timestamp is concerned. It's showing only the date. So does this mean it's a real date only information?
However, it might be more efficient to do two comparisons, especially if there's an index on dt:
WHERE dt >= TO_DATE ('21/01/2010', 'DD/MM/YYYY')
AND dt < TO_DATE ('22/01/2010', 'DD/MM/YYYY')
Ok. If we change the range to compare it on the same day, it won't work;
WHERE dt >= TO_DATE ('21/01/2010', 'DD/MM/YYYY')
AND dt <= TO_DATE ('21/01/2010', 'DD/MM/YYYY')this also happens if we're using BETWEEN
this is real weird. It's supposed to work...i don't recall having this kind of issue on other databases. If that's the case,
each time a user passes me a date string i have to somehow add another day onto its 'to' comparison date in order for things to work.
but..i don't want to since i don't want to accidentally include the extra day's results!
Edited by: 803998 on Oct 21, 2010 8:14 PM
Edited by: 803998 on Oct 21, 2010 8:33 PM -
** How to use TO_DATE function in Stored Proc. for JDBC in ABAP-XSL mapping
Hi friends,
I use ABAP-XSL mapping to insert records in Oracle table. My Sender is File and receiver is JDBC. We use Oracle 10g database. All fields in table are VARCHAR2 except one field; this is having type 'DATE'.
I use Stored procedure to update the records in table. I have converted my string into date using the Oracle TO_DATE function. But, when I use this format, it throws an error in the Receiver CC. (But, the message is processed successfully in SXMB_MONI).
The input format I formed like below:
<X_EMP_START_DT hasQuot="No" isInput="1" type="DATE">
Value in Payload is like below.
<X_EMP_START_DT hasQuot="No" isInput="1" type="DATE">TO_DATE('18-11-1991','DD-MM-YYYY')</X_EMP_START_DT>
Error in CC comes as below:
Error processing request in sax parser: Error when executing statement for table/stored proc. 'SP_EMP_DETAILS' (structure 'STATEMENT'): java.lang.NumberFormatException: For input string: "TO_DATE('18"
Friends, I have tried, but unable to find the correct solution to insert.
Kindly help me to solve this issue.
Kind Regards,
Jegathees P.
(But, the same is working fine if we use direct method in ABAP-XSL ie. not thru Stored Procedure)Hi Sinha,
Thanks for your reply.
I used the syntax
<xsl:call-template name="date:format-date">
<xsl:with-param name="date-time" select="string" />
<xsl:with-param name="pattern" select="string" />
</xsl:call-template>
in my Abap XSL. But, its not working correctly. The problem is 'href' function to import "date.xsl" in my XSLT is not able to do that. The system throws an error. Moreover, it is not able to write the command 'extension-element-prefixes' in my <xsl:stylesheet namespace>
May be I am not able to understand how to use this.
Anyway, I solved this problem by handling date conversion inside Oracle Stored Procedure. Now, its working fine.
Thank you. -
The to_date function doesn't work ?
Hello
I don't know why my to_date function doesn't work on my pc.
my statement is pretty complex so i just tried simple one
select to_date('10-Jan-2006','dd-mon-yyyy') from dual;
but even this one doesn't work it says it is invalid month
howcome?
is it because my Windows XP (not english version) doesn't recognise month Jan?
i tried it another place it worked
so is there any language pack / patch i need to install? ?
so does OS matters?
or how to fix this..please help meBy default SQL Developer picks up it's language settings up from (on Windows) the Regional and Language Options. You can see what it has set by querying the NLS_SESSION_PARAMETERS view.
Assuming you don't want to change to using DD-MM-YYYY formats for your months or change your regional settings on your PC, you need to tell SQL Developer which language you want, which you can do by adding the following lines to the file sqldeveloper\jdev\bin\sqldeveloper.conf:
AddVMOption -Duser.language=en
AddVMOption -Duser.country=US -
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.
erHello,
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? -
How to use quarter format in "to_date()" function?
Hi,
I'm trying to use to_date() function with quarter format. How do I specify the format?
For example to convert '2002 quarter 1' using the to_date function.
I tried to_date('20021', 'YYYYQ'), and I got ORA-01820 cannot appear in date input format.
Please help, thank you!!!You cannot use Q when inputting data, only when retrieving data. When inputting, you must enter a valid date. Oracle has to store a date and if you just enter a quarter, it can't figure out what date to store. See the examples below:
SQL> -- test table:
SQL> CREATE TABLE test_table
2 (test_date DATE)
3 /
Table created.
SQL> -- the wrong way to input:
SQL> INSERT INTO test_table (test_date)
2 VALUES (TO_DATE ('2002-1', 'YYYY-Q'))
3 /
VALUES (TO_DATE ('2002-1', 'YYYY-Q'))
ERROR at line 2:
ORA-01820: format code cannot appear in date input format
SQL> -- the right way to input:
SQL> INSERT INTO test_table (test_date)
2 VALUES (TO_DATE ('27-OCT-2002', 'DD-MON-YYYY'))
3 /
1 row created.
SQL> -- to output:
SQL> COLUMN "Year and Quarter" FORMAT A16
SQL> SELECT test_date,
2 TO_CHAR (test_date, 'YYYY-Q') AS "Year and Quarter"
3 FROM test_table
4 /
TEST_DATE Year and Quarter
27-OCT-2002 2002-4
1 row selected. -
To_date function not fetching the desired result.....
Hi Gurus,
I get varied results from the below 2 queries, I am pessimistic about using to_char instead prefer using to_date, but query doesn't seem to fetch the complete data while using the to_date but with to_char it works. Please can someone make this to_date used query below work for me.
Main table structure:
Column Name ID Pk Null? Data Type Default Histogram Encryption Alg Salt
MINISTRY_CODE 1 Y VARCHAR2 (15 Byte) Yes
BILL_MONTH 2 Y DATE Yes
CUBIC 3 Y NUMBER Yes
GALLONS 4 Y NUMBER Yes
AMOUNTS 5 Y NUMBER Yes
MTR_TYPE 6 Y CHAR (1 Byte) Yes 1st query:
SELECT ministry_code,
DECODE(mtr_type, 'C','Billed Cubic',
'G','Billed Gallons',
'A','Billed OMR',
'R','Collected',
'U','Total Unpaid Bills') mtr_type,
SUM(CASE
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'C' THEN cubic
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'G' THEN (gallons)
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '012011' AND mtr_type = 'A' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') = '022011' AND mtr_type = 'R' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month,'MMYYYY') <= '022011' AND mtr_type = 'U' THEN amounts ELSE 0 END) January
FROM tmp_paew_month_gafu1 WHERE ministry_code IN
(SELECT DISTINCT ministry_code FROM tmp_paew_month_gafu GROUP BY ministry_code)
GROUP BY ministry_code,mtr_type
ORDER BY 1,3 DESCSAMPLE OUTPUT:
MINISTRY_CODE MTR_TYPE JANUARY
001 Billed Gallons 5326252
001 Billed Cubic 24210.2363636364
001 Total Unpaid Bills 16402.5
001 Billed OMR 13287.36
001 Collected 10410.508
002 Total Unpaid Bills 12089.99
002 Billed Cubic 0
002 Billed OMR 0
002 Billed Gallons 0
002 Collected 0
003 Total Unpaid Bills 27418.711
2nd query:
SELECT ministry_code,
DECODE(mtr_type, 'C','Billed Cubic',
'G','Billed Gallons',
'A','Billed OMR',
'R','Collected',
'U','Total Unpaid Bills') mtr_type,
SUM(CASE
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_char(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'C' THEN cubic
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'G' THEN (gallons)
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('012011','MMYYYY') AND mtr_type = 'A' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) = to_date('022011','MMYYYY') AND mtr_type = 'R' THEN amounts
WHEN mtr_type NOT IN ('C','G','A','R','U') THEN 0 WHEN to_date(bill_month) <= to_date('022011','MMYYYY') AND mtr_type = 'U' THEN amounts ELSE 0 END) January
FROM tmp_paew_month_gafu1 WHERE ministry_code IN
(SELECT DISTINCT ministry_code FROM tmp_paew_month_gafu GROUP BY ministry_code)
GROUP BY ministry_code,mtr_type
ORDER BY 1,3 DESCSample OUTPUT:
MINISTRY_CODE MTR_TYPE JANUARY
001 Total Unpaid Bills 106062.002
001 Billed OMR 0
001 Billed Cubic 0
001 Billed Gallons 0
001 Collected 0
002 Total Unpaid Bills 42023.243
002 Collected 0
002 Billed Gallons 0
002 Billed Cubic 0
002 Billed OMR 0
003 Total Unpaid Bills 133260.435What is the sense of to_date(bill_month) in the second query?
bill_month is already a date. to_date function will expect a character parameter and convert it in a date value.
So what will happen? First the bill_month will be implicitely converted to a character value.
Second this character value will be converted back to a date. You gave no format string. So both conversions will be made will your setting of nls_date_format.
I can only guess what you really want. Perhaps you want to trunc the bill_month with trunc(bill_month,'MM').
Please give us
- table create script
- insert statements for test data
- required output for these test data
- description of your requirements
Edited by: hm on 22.11.2012 23:16 -
Equivalent of to_date function in Ms SQL and using it in Evaluate function
Hi,
I am trying to find out a function in MS SQL which is equivalent to to_date function in oracle. Please find below the advanced filter i am trying to use in OBIEE.
Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY')>=Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY') and Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY') <=timestampadd(sql_tsi_month,4,Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY'))
The statement above works fines with oracle DB with to_date function. The same statement throws an error with MS SQL since to_date is not a built in function.
With MS SQL I tried with CAST, not sure how to pass parameters %1 and %2.
Please help me how to use Evaluate function and passing parameters along with to_date funtion in MS SQL.
Regards!
RRHi,
please refer to this thread for useful information on using to_char and to_date functions of oracle in MS SQL server:
http://database.ittoolbox.com/groups/technical-functional/sql-server-l/how-to-write-to-to_char-and-to_date-sql-server-351831
Hope this helps.
Thanks,
-Amith. -
TO_DATE Function Is not working
Hi Friends,
select * from view_date where to_date(my_date) between '01-dec-06' and '31-dec-06';
This query is not responding...
I have the the date field in character in the view so i need to convert it to date ,so that iam using to_date function and its not responding.
Without using to_date iam getting the results but not the exact results,it includes all the record sets.
Help me in thisTO_DATE converts a string to a date. TO_CHAR converts a date to a string. What is my_date?
Edit: Just realised you said "my_date" is a string.
Try
select * from view_date
where TO_DATE(my_date,'???')
between TO_DATE('01-dec-2006','dd-mon-yyyy') and TO_DATE('31-dec-2006','dd-mon-yyyy');or if you are dealing with literals you can use the more compact ANSI format:
select * from view_date
where TO_DATE(my_date,'???') between DATE '2006-12-01' and DATE '2006-12-31';Avoid 2-digit years. You have no idea the problems that caused a few years back.
Message was edited by:
William Robertson
Oops, typed stuff the wrong way around, need coffee... -
To_date function in cursor in oracle 8i
Please tell me how to use to_date in cursor in oracle 8i. when i m using to_date in where clause in cursor it's giving error to_date function is out of scope.
CURSOR C1 IS
SELECT A.XO_NO,to_char(A.XO_DATE,'dd-mm-yyyy') XO_DATE,A.TO_CITY,A.FILE_CODE F_CODE,A.ARR_DATE,A.DEP_DATE,A.VCH_TYPE_CODE,
A.VCH_NO,A.REMARKS,A.AMOUNT,A.COMP_CODE,to_char(A.SERVICE_DATE,'dd-mm-yyyy')SERVICE_DATE,
B.AGENT_CODE,B.ROOM_TYP_CODE,
(B.NO_OF_SINGLE+B.NO_OF_DOUBLE*2+B.NO_OF_EX_BED+B.NO_OF_TWIN*2) NO_OF_PAX,
B.PER_SR SNGL_BED,B.PER_DR DOUBLE_BED,B.PER_EB TWIN,B.PER_DR EXTRA_BED,B.MEAL_PLAN,
B.EXCH_RATE,A.FILE_CODE,C.COMP_NAME
FROM XOS_MS A,QUOT_HOTEL_DS B,AC_COMPANY_MS C
WHERE A.COMP_CODE=B.COMP_CODE
AND A.XO_NO=B.XO_NO
AND A.COMP_CODE=C.COMP_CODE
AND TRUNC(A.XO_DATE)=TO_DATE('21/03/2005','DD/MM/YYYY'); -
Case sensitive field in to_date function
update "Ab_Order" set "OrderDate" = to_date("Order Date String", 'yyyy/mm/dd)
Here the OrderDate column is of type Date
"Order Date String" is varchar2. Iam trying to copy the data from "Order Date String" which contains data in this format '2011/04/24' to OrderDate column to perform some date functions
when I try to run this query it gives a error: ORA-01830: date format picture ends before converting entire input string
So I tried to run this query by creating a test table wit 2 columns : update testtable set column1 = to_date(field1, 'yyyy/mm/dd')
where column1 is date field and field1 is varchar2 and this worked.
So Iam assuming this is the problem with case sensitive fieldnames . As far as i know we should include case sensitive fields in double quotes. Is there anything else to do wit the to_Date function. Can anyone please let me know how to do that.Hi,
Welcome the the forum!
882431 wrote:
update "Ab_Order" set "OrderDate" = to_date("Order Date String", 'yyyy/mm/dd)It looks like you're missing a single-quote right before the last ')'.
Here the OrderDate column is of type Date
"Order Date String" is varchar2. Iam trying to copy the data from "Order Date String" which contains data in this format '2011/04/24' to OrderDate column to perform some date functions
when I try to run this query it gives a error: ORA-01830: date format picture ends before converting entire input stringThat error occurs when when you have characters in the 1st argument that do not correspond to anything in the 2nd argument. For example:
TO_DATE ( '30-Aug-2011 12:00'
, 'dd-Mon-yyyy'
)In this example, TO_DATE doesn;t know what to do with the ' 12:00' at the end of the 1st argument. According to the 2nd arguemnt, there's only supposed to be 11 characters in the string. So it raises the ORA-01830 error.
So I tried to run this query by creating a test table wit 2 columns : update testtable set column1 = to_date(field1, 'yyyy/mm/dd')
where column1 is date field and field1 is varchar2 and this worked.
So Iam assuming this is the problem with case sensitive fieldnames . As far as i know we should include case sensitive fields in double quotes. Is there anything else to do wit the to_Date function. Can anyone please let me know how to do that.Case sensitive column names are a bad idea because they cause so amny problems, but I don't think this is one of those problems. It's more likely that you need to use SUBSTR (or some other string manipulation function) on "Order Date String" before using it in TO_DATE.
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables, so people can re-create the problem and test their ideas.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Always say which version of Oracle you're using. -
Sql query to_date function
hi all,
I am working on a project where some sql scripts are already been developed by
someone else.
In the code i found
Select col_name
from table_name
where txn_dt between to_date(to_date('05-dec-2007') - 6) and to_date('05-dec-2007');
I am wondering whats the usage of 2 to_date function in the where clause.
This can be achieved using this as well:
Select col_name
from table_name
where txn_dt between to_date('05-dec-2007') - 6 and to_date('05-dec-2007');
Can anybody tell me this diff if there is any?
Thanks
Shaneto_date(to_date('05-dec-2007') - 6)This is an error. Not a syntax error, but a runtime error waiting to happen.
The inner to_date is fine (except it should have a format mask) but the outer to_date is expecting a character type and it's getting a date. So it has to be converted.
All this depends on the nls settings which could change.
Ideally, the code should read:
Select col_name
from table_name
where txn_dt between to_date('05-dec-2007','dd-mon-yyyy') - 6 and to_date('05-dec-2007','dd-mon-yyyy');
(on second thought...if the nls settings match this date format, it should convert without error in both to_date functions, but still...why do it?)
Edited by: SomeoneElse on Dec 12, 2008 9:12 AM -
To_date function in procedure which returns a cursor type
Hi All
When i execute the following query ,it runs fine and i get required result also
select *from a where adate>to_date('31-dec-2001')
But When i use the same query in a procedure which returns a cursor i get a error
"non numeric character was found where a numeric value was expected".
Then i changed the query to
select *from a where adate>to_date('31-dec-2001','dd-mon-yyyy') and it worked fine.
As i know default date format is 'dd-mon-yyyy' and no need to give the date format
explicitly in to_date function if the date(string) is in default format.
Can any body tell me the reason behind it?
Thanks
SatyaGiven a comparison between a date and a string Oracle will convert the string to a date. This means that if you have an index on a string column and compare it to a date the index will not be available, but if you have an index ona date column and compare it to a string column the index will be available.
create table t (string varchar2(30), d date);
insert into t values (to_char(sysdate), sysdate);
commit;
create index t_n1 on t(string);
create index t_n2 on t(d);
SQL> set autotrace traceonly
SQL> select /*+ index t_n1 */
2 *
3 from t
4 where string = sysdate;
no rows selected
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
SQL> select /*+ index t_n2 */
2 *
3 from t
4 where d = to_char(sysdate);
no rows selected
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'T'
2 1 INDEX (RANGE SCAN) OF 'T_N2' (NON-UNIQUE)
SQL> -
Dear All,
I need Output of the following query in the 'JANUARY' or 'JAN' i.e. 'MONTH' or 'MON' format. But following query is returning full date. Could any person help to get me output in MONTH or MON format but SQL should be using TO_DATE function.
TO_DATE(to_char(trunc(ORDERED_DATE),'MONTH'),'MONTH') order_date
Above query is failing to return desired output.
ThanksFor each DATE value, Oracle stores the following information: century, year, month, date, hour, minute, and second.
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements001.htm#sthref116
there are some conversion functions :
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions180.htm#i1009324
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions183.htm#i1003589
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions201.htm#i79761
moreover, the human eye can only read character datatypes (not number or date datytypes) !
which means, whenever you do a select sysdate from dual there will be an implicit conversion
to character datatype to make it readable for you (according to nls_date_format) .
alter session set nls_date_format = 'DD.MM.YYYY' ;
select sysdate today from dual ;
TODAY
21.12.2008
alter session set nls_date_format = 'YYYY.MM.DD' ;
select sysdate today from dual ;
TODAY
2008.21.12So the same statement select sysdate today from dual ; will give different results on different systems,
just depending on some initial settings !
You ask why ? It's because you are using implicit conversion !!!
Re: Sql Error -
To_date function now fails
I have a package that ran successfully for years. This morning it fails with a ORA-01858: a non-numeric character was found where a numeric was expected.
It fails on the to_date function ie to_date(SYSDATE,'RRRRMMDD').
Why is it failing now and did not fail previously?Kiran wrote:
then you have to write as to_Date(to_char('21/02/2012','RRRRMMDD'),'RRRRMMDD')NO!
just as sysdate is already a date and is not passed to to_date, the above is already a character string and is not passed to to_char.
And your format mask is wrong. '21/02/2012' is not in the format '21/02/2012'
It should be to_date(''21/02/2012','dd/mm/yyyy').
Maybe you are looking for
-
Error while trying to create a table in SQL
I am fairly new to Visual Studio. I am using the Visual Studio 2012. So I had to create a project in MVC. I made the Views and the controllers. I did add a Database from, _AppDate->Add->New Item-> SQL Server Compact 4.0 Local Database. Now that I am
-
HI all, I created data definition and template for POXPRPOP ==> Printed Purchase Order Report(Portrait), sat up output to XML in Request form for POXPRPOP and ran it. When trying to preview, I am getting the error bellow followed by template page. Wh
-
Where is the BPEL Instance payload stored in soa database?
Hi all, Given an instance id, I need to get the payload used to invoke the BPEL. I searched in the soa database. Also I went through the bpel client apis. But I couldn't find any. Is there any table in the BPEL dehydration store that can give me the
-
Do I need to have license to use BI Publisher in E-Business suite?
Hi To use BI Publisher, do we nee to purchase license from oracle(Either individual or a company)? Regards BKR.
-
Whi isn't my Ipad able to connect to wifi when I was able to previously?
A few days ago I had been able to connect to my home Wifi and now I can't get onto the net. Why and what can I do to get my Wifi back on line?