Convert Varchar2 to Date
Hello
I have a column value Scaned_date = 2009-09-25 21:41:59.892
that i need to convert it into Date please suggest i am geeting errors in this
Thanks
Hi,
A user-defined fucntion like this can help you find the bad data:
CREATE OR REPLACE FUNCTION to_dt
( in_txt IN VARCHAR2 -- to be converted
, in_fmt_txt IN VARCHAR2 DEFAULT 'DD-MON-YYYY' -- optional format
, in_err_dt IN DATE DEFAULT NULL
RETURN DATE
DETERMINISTIC
AS
-- to_dt attempts to convert in_txt (assumed to
-- be in the format of in_fmt_txt) to a DATE.
-- If the conversion works, to_dt returns the DATE.
-- If the conversion fails for any reason, to_dt returns in_err_dt.
BEGIN
-- Try to convert in_txt to a DATE. If it works, fine.
RETURN TO_DATE (in_txt, in_fmt_txt);
EXCEPTION -- If TO_DATE caused an error, then this is not a valid DATE: return in_err_dt
WHEN OTHERS
THEN
RETURN in_err_dt;
END to_dt
/Here's how you might use it:
SELECT primary_key
, scanned_date
FROM table_x
WHERE scanned_date IS NOT NULL
AND to_dt ( SUBSTR (scanned_date, 1, 19)
, 'YYYY-MM-DD HH24:MI:SS'
) IS NULL
;iIf you need help, post a little sample data (CREATE TABLE and INSERT statements), the results you want to get from that data, and your complete query that gets an error with the sample data.
Similar Messages
-
Converting varchar2 to date format
I've converted date formats many times, but for some reason I'm getting an invalid number error when trying to convert a varchar2 column. I've tried the to_char and to_date function and I get the same result. The column is a date and it is formatted as DD-MON-YYYY, but I want to change it to MM/DD/YYYY. My query is below:
select to_date('fccpdate','MM/DD/YYYY')
from cc_class_scmast_v
When I try to_date I get this:
Error starting at line 1 in command:
select TO_DATE('fccpdate', 'DD-MON-YYYY') from cc_class_scmast_v where fccpdate IS NOT NULL
Error report:
SQL Error: ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
When I try to_char I get this:
Error starting at line 1 in command:
select TO_char('fccpdate', 'DD-MON-YYYY') from cc_class_scmast_v where fccpdate IS NOT NULL
Error report:
SQL Error: ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
I've tried removing the single quotes from my column and that doesn't make a difference. Any help is appreciated.Hi,
housetiger77 wrote:
I've converted date formats many times, but for some reason I'm getting an invalid number error when trying to convert a varchar2 column. I've tried the to_char and to_date function and I get the same result. The column is a date and it is formatted as DD-MON-YYYY,If the column is a DATE, then it has the same format that all DATEs have, which is nothing like 'DD-MON-YYYY'. Formats like that only apply to strings.
Conversely, if it is formatted as 'DD-MON-YYY', then it is a string, not a DATE.
but I want to change it to MM/DD/YYYY. My query is below:
select to_date('fccpdate','MM/DD/YYYY')
from cc_class_scmast_vTO_DATE (x, 'MM/DD/YYYY') tries to convert the string x into a DATE. Let's say it starts by taking the first 2 characters of x, to get the month. The first 2 charcters of 'fccpdate' are 'fc', which is not a valid number (at least not in base 10), let alone a number between 1 and 12, so TO_DATE raises an error.
When I try to_date I get this:
Error starting at line 1 in command:
select TO_DATE('fccpdate', 'DD-MON-YYYY') from cc_class_scmast_v where fccpdate IS NOT NULL
Error report:
SQL Error: ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
When I try to_char I get this:
Error starting at line 1 in command:
select TO_char('fccpdate', 'DD-MON-YYYY') from cc_class_scmast_v where fccpdate IS NOT NULL
Error report:
SQL Error: ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
I've tried removing the single quotes from my column and that doesn't make a difference. Any help is appreciated.That's a good first step. Literals are enclosed in single-quotes, identifiers (including column names) are not. 'fccpdate' is the literal 8-character string containing 'f', 'c;, another 'c', 'p', 'd', 'a', 't' and 'e'. fccpdate (without single-quotes) can be the name of a column.
If fccpdate is a string, such as '18-JUL-2012', then you can convert it to a DATE using TO_DATE.
TO_DATE (fccpdate, 'DD-MON-YYYY')If you want to display a DATE in a particular format, use
TO_CHAR ( d
, f
)where d is a DATE, and f is the format string. In this case, d might be the TO_DATE expression above
TO_CHAR ( TO_DATE (fccpdate, 'DD-MON-YYYY')
, 'MM/DD/YYYY'
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
See the forum FAQ {message:id=9360002} -
How to Convert Varchar2 to Date format(Timestamp)
I have a date saved in varchar2 column.
how can I convert the values into date(including Time stamp), so I can use the date to compare data.
Ex: I have the value '20-03-2007 05:31:29', but this value is saved as varchar2.
how can I take from this value the date '20-03-2007 05:31:29' as date format because later i need that date completely to add days, hours and minutes?SQL> create table dd (a varchar2(15));
Table created.
SQL>
SQL>
SQL> insert into dd values (sysdate);
1 row created.
SQL> insert into dd values (sysdate);
1 row created.
SQL> insert into dd values (sysdate);
1 row created.
SQL> insert into dd values (sysdate);
1 row created.
SQL> insert into dd values (sysdate);
1 row created.
SQL> insert into dd values (sysdate);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> select * from dd;
A
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
6 rows selected.
SQL>
SQL>
SQL> select to_date(a,'dd-mon-yyyy') from dd;
TO_DATE(A
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
01-OCT-12
6 rows selected.
SQL> select to_timestamp(to_date(a,'dd-mon-yyyy'),'dd mon yy hh24.mi.ss' ) from dd;
TO_TIMESTAMP(TO_DATE(A,'DD-MON-YYYY'),'DDMONYYHH24.MI.SS')
01-OCT-12 12.00.00 AM
01-OCT-12 12.00.00 AM
01-OCT-12 12.00.00 AM
01-OCT-12 12.00.00 AM
01-OCT-12 12.00.00 AM
01-OCT-12 12.00.00 AM
6 rows selected.
SQL>
that's it...... -
Convert varchar2 to date in oracle
how to covert varchar2 (30) to date fromat
example '01/01/2007' to 01/01/2007
thanks in advance
ReddyI want to insert into my new table the data in the
form DD-MON-RRRRIf your new table has the data type as DATE then you don't insert dates in any particular format, you just insert values that are DATE datatypes.
The format is only important when you view the data.
e.g.
SQL> create table dt (mydate DATE);
Table created.
SQL> insert into dt values (to_date('01/01/2008','DD/MM/YYYY'));
1 row created.
SQL> insert into dt values (to_date('01-FEB-2008','DD-MON-YYYY'));
1 row created.
SQL> insert into dt values (to_date('20080402','YYYYMMDD'));
1 row created.
SQL> select * from dt;
MYDATE
01/01/2008 00:00:00
01/02/2008 00:00:00
02/04/2008 00:00:00
SQL> alter session set nls_date_format='DD-MON-YYYY';
Session altered.
SQL> select * from dt;
MYDATE
01-JAN-2008
01-FEB-2008
02-APR-2008
SQL>Edit:
Sorry, forgot to say...
This just demonstrates that any format of string can be inserted into a date column if the format string is specified correctly and it also demonstrates that what is stored is just a date, the format of which is determined by the settings of the tool you are using to retrieve the data (unless you specify e.g. to_char(date_column, 'DD-MON-YYYY') in the SELECT statement to convert it to a string again manually)
Message was edited by:
BluShadow -
Convert Varchar2 datatype Date Value To Date Datatype
Hello,
I have a date value like the following as varchar2 datatype.
31-DEC-10 08.40.53 AMI would like to convert this to_date as date datatype.
How could I do this?Use the right format mask?
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions183.htm#SQLRF06132
SQL> select to_date('31-DEC-10 08.40.53 AM', 'dd-mon-yy hh.mi.ss am')
2 from dual;
TO_DATE('31-DEC-100
31-12-2010 08:40:53
1 row selected.
SQL> declare
2 dt date;
3 begin
4 dt := to_date('31-DEC-10 08.40.53 AM', 'dd-mon-yy hh.mi.ss am');
5 dbms_output.put_line(to_char(dt, 'dd-mm-yyyy hh24:mi:ss'));
6 end;
7 /
31-12-2010 08:40:53
PL/SQL procedure successfully completed. -
I need to use a to_date on a VARCHAR2 field that is being stored like 2006-08-01 00:00:00.0 My problem is lying in the millesecond. What is the proper format for the to_date function?
1) traditional Oracle dates do not include anything smaller than a second.
2) If you provided your Oracle version we might be abkle to propose solutions. For example, newer versions have timestamps that might help.
3) the SQL Reference manual at http://docs.oracle.com, Chapter 2, goes into excrutiating detail about format masks and Chapter 5 deals with functions including to_date. It's got examples as well. For 10g it's http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/toc.htm -
Hi,
I have a table with a date field that comes as varchar2(20byte) with a content like this:
20/Jan/2011
and I need it to be converted toa real date format e.g. 20.01.2011.
Please help,
Thanks
WalterHi, Walter,
user457173 wrote:
Hi,
I have a table with a date field that comes as varchar2(20byte) with a content like this:
20/Jan/2011
and I need it to be converted toa real date format e.g. 20.01.2011.'20/Jan/2011' is actually just as "real" as '20.01.2011'. Neither is a real DATE; both are strings.
Please help,
Thanks
WalterUse TO_DATE to convert a string into a DATE:
TO_DATE ( column_x
, 'DD/Mon/YYYY'
)Use TO_CHAR to display a DATE in a given format:
TO_CHAR ( d
, 'DD.MM.YYYY'
)d can be any DATE, including the DATE returned by the TO_DATE function, above.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using.
Edited by: Frank Kulash on Oct 25, 2011 7:57 AM -
Hi
im trying to convert varchar to date.
So, i used To_date ( cilumn, 'DD-MM-YYYY') , Since i have to order the out put in Decending Order i used the same convertion function with Order by clause.
The problem is ... the out put is not ordered,
<sql>
select date_created
from
select distinct
to_date(created_date,'DD-MM-YYYY') as date_created
from schedule_backup
order by date_created desc
<sql>
out put is
=================
DATE_CREA
26-NOV-09
23-NOV-09
19-NOV-09
16-NOV-09
11-NOV-09
04-NOV-09
30-OCT-09
11-SEP-09
04-MAR-10
01-MAR-10
25-FEB-10
DATE_CREA
22-FEB-10
17-FEB-10
15-FEB-10
11-FEB-10
09-FEB-10
03-FEB-10
27-JAN-10
20-JAN-10
15-JAN-10
04-JAN-10
thanks
rajHi, Raj,
When you store dates in a VARCHAR2 column, you're asking for touble, so it seems that everything is working according to plan.
Even aside from that, your query works perfectly for me.
In the output you posted, the order is correct within each year, and you're only showing 2 digits of the year. Are you sure the centuries are right in the schedule_backup table? Perhaps everything that was supposed to be '2010' got enetereed as '1910' or '0010'.
Post some sample data (CREATE TABLE and INSERT statements) that gets the wrong results with your query.
For example:
CREATE TABLE schedule_backup
( created_date VARCHAR2 (10)
INSERT INTO schedule_backup (created_date) VALUES ('11-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('30-10-2009');
INSERT INTO schedule_backup (created_date) VALUES ('11-09-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-03-2010');
COMMIT; -
Convert VARCHAR to date - don't know format in advance
Hi,
If I want to convert varchar to date, but I don't kniow the format of the varchar in advance. For example, it may be '20-MAY-99', '2009-01-10 10:01:00', but it is not known in advance. How can I convert it to date?
Many thanksYou'll have to define a hierarchy of date formats to try. There are some strings that map to many different valid dates depending on the format. The string '01/02/03' might translate to January 2, 2003, January 2, 1903, February 1, 2003, February 1, 1903, February 3, 2001, February 3, 1901, etc.
Assuming you define a hierarchy of formats, you can write a function that tries each format in turn, i.e.
CREATE FUNCTION my_to_date( p_string IN VARCHAR2 )
RETURN DATE
IS
l_date_variable DATE;
BEGIN
l_date_variable := to_date( p_string, <<format 1>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
BEGIN
l_date_variable := to_date( p_string, <<format 2>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
END;You'll need to define the N date formats to try and the order in which to try them.
Justin -
Hi All
when i run the below select statement i get error message. pay_dt is varchar2(8) data type.
ORA-01861 : literal does not match format string
Any help on this appreciated. Thank you very much.
SELECT DISTINCT to_char(to_date(pay_dt
,'YYYY-MM-DD')
,'MON')
from paymentsMahir M. Quluzade wrote:
Your pay_dt datatype varchar2(8), but your gived format YYYY-MM-DD length is 10.
Change yor format please to 'YYYYMMDD' or 'DDMMYYYY' or 'MMDDYYYY' or 'YYYYDDMM' for your payments.pay_dt data.
for examle : pay_dt is '20010528' then use 'YYYYMMDD'unfortunately probably not the case - oracle is surprisingly forgiving with dates and will ignore any separators you add in your format mask if it can still convert it.
SQL> create table foo (dte varchar2(8));
Table created.
Elapsed: 00:00:00.00
SQL> insert into foo values ('20110101');
1 row created.
Elapsed: 00:00:00.00
SQL>
SQL> select to_date(dte,'yyyy-mm-dd') from foo;
TO_DATE(DTE,'YYYY-M
2011-01-01 00:00:00
1 row selected.I'd recommend searching through the data to find the record(s) that are completely around the wrong way or contain invalid characters etc. -
Need to convert the binary data in blob field to readable format
Hi,
We need to convert the Binary data in the BLOB field to readable format and need to display it in Oracle Apps Environment,does anybody come across the similar requirement.
please advise, thanks in advance.
Regards,
Babu.You could use standard Attachments functionality here ... if the blob is not in FND_LOBS, insert into FND_LOBS, fnd_attached_documents etc to enable viewing via "Attachments" to the entity the blob is related to.
Gareth -
Need to Convert Comma separated data in a column into individual rows from
Hi,
I need to Convert Comma separated data in a column into individual rows from a table.
Eg: JOB1 SMITH,ALLEN,WARD,JONES
OUTPUT required ;-
JOB1 SMITH
JOB1 ALLEN
JOB1 WARD
JOB1 JONES
Got a solution using Oracle provided regexp_substr function, which comes handy for this scenario.
But I need to use a database independent solution
Thanks in advance for your valuable inputs.
GeorgeGo for ETL solution. There are couple of ways to implement.
If helps mark -
Convert String to Date and Format the Date Expression in SSRS
Hi,
I have a parameter used to select a month and year string that looks like: jun-2013
I can convert it to a date, but what I want to do is, when a user selects a particular month-year (let's say "jun-2013")
I populate one text box with the date the user selected , and (the challenge Im having is) I want to populate a text box next to the first text box with the month-year 2 months ahead. So if the user selects
jun-2013 textbox A will show jun-2013
and textbox B will show aug-2013..
I have tried:
=Format(Format(CDate(Parameters!month.Value ),
"MM-YYYY" )+ 2 ) -- But this gives an error
This returns the month in number format like "8" for august...
=Format(Format(CDate(Parameters!month.Value ),
"MM" )+ 2 )
What is the proper syntax to give me the result in this format = "aug-2013" ???
Thanks in advance.
MC
M CollierYou can convert a string that represents a date to a date object using the util.scand JavaScript method, and then format a date object to a string representation using the util.printd method. For more information, see:
http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1254.html
http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1251.html
In your case, the code would be something like:
var sDate = "2013-01-10";
// Convert string to date
var oDate = util.scand("yyyy-mm-dd", sDate);
// Convert date to new string
var sDate2 = util.printd("mm/dd/yyyy", oDate);
// Set a field value
getField("date2").value = sDate2;
The exact code you'd use depends on where you place the script and where you're getting the original date string, but this should get you started. -
Convert Char to Date format - Evaluate
Hi,
Could anyone provide us the Evaluate formula to convert Char to Date format
2009-06-20 should be converted to 06/20/2009
Regards,
VinayHi,
Refer the below threads...
Re: How to convert string to date format?
how to convert character string into date format????
Regards,
Chithra Saravanan -
Convert Char to Date in SQL Server
Hello Experts,
I am trying to convert Char to Date but getting error in Universe designer. Can anybody advise please?
Thanks,
RaviHi,
Try with CAST() and CONVERT() functions. For more information refer use this url : http://msdn.microsoft.com/en-us/library/ms187928.aspx.
It is more easy to get solution if you can post your query.
Cheers,
Suresh Babu Aluri.
Maybe you are looking for
-
OBIEE variables in custom java script
Hello all Can I use OBIEE variables like presentation and session variables in my custom java script code that I am writing in text view on a dashboard page. Is it possible to do that. please me soon if somebody knows the solution to do that. Thanks
-
Case insensitive search in select statement
Hi Experts, I have entries in table as mixed case. For example i am searching on an attribute called description in the table. I want the serch to be case insensitive for that attribute. Because in table it is i.e. JUMBO Pack and in the search attr
-
Hi there, I made a CD booklet and submitted it to an internet print service as a pdf. I was told the document had to be changed from RGB to CMYK 8for ten bucks). Ok. The printout turned out to be very different from the proof print I had made at a lo
-
Significant performance degradation after TM restore
I recently installed a new internal HD in my iMac. While everything seems to work from a hardware standpoint, the performance has significantly degraded. Specifically, the screen locks up from time to time for several seconds. I think I have isolated
-
Video Streaming stalls on Macbook Pro
I have been having issues with streaming. I thought it was my internet connection, then my wife tried the same video (nfl.com) with his laptop PC and it played it just fine! Why are we having streaming video issues with macbook pro?! It's brand new!!