Home » SQL & PL/SQL » SQL & PL/SQL » convert char to date (Oracle 11g)
convert char to date [message #616865] Sat, 21 June 2014 14:25 Go to next message
mape
Messages: 298
Registered: July 2006
Location: Slovakia
Senior Member

Hi

I'd would like to know to get a righ date format of string for example 20140501130926 ?
I need to conver into the format dd.mm.yyyy h24:mi:ss

This varchar string 20140501130926 consist of number of data yyyymmddh24miss

I tried to use to_date(20140501130926, 'yyyymmdd h24:mi:ss') or to_char(20140501130926, 'yyyymmdd h24:mi:ss')
but unsuccessfully.

Thanks for help.

Regards
Martin
Re: convert char to date [message #616869 is a reply to message #616865] Sat, 21 June 2014 14:36 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
TO_DATE is the right choice. You can't use format mask that doesn't exist in input string. Where do you see colons in 20140501130926? There are none!

SQL> create table test (some_date date);

Table created.

SQL> insert into test (some_date)
  2    values (to_date('20140501130926', 'yyyymmddhh24miss'));

1 row created.

SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';

Session altered.

SQL> select * from test;

SOME_DATE
-------------------
01.05.2014 13:09:26

SQL>
Re: convert char to date [message #616870 is a reply to message #616869] Sat, 21 June 2014 14:45 Go to previous messageGo to next message
mape
Messages: 298
Registered: July 2006
Location: Slovakia
Senior Member

You're right, after set of nls_date_format = 'dd.mm.yyyy hh24:mi:ss'
it works fine.

Thanks
Regards
Re: convert char to date [message #616924 is a reply to message #616869] Mon, 23 June 2014 02:45 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Littlefoot wrote on Sat, 21 June 2014 20:36
You can't use format mask that doesn't exist in input string. Where do you see colons in 20140501130926? There are none!

Oracle is very forgiving about missing separators. The problem in this case is that h24 isn't valid:
SQL> SELECT to_date(20140501130926, 'yyyymmdd h24:mi:ss') FROM dual;
 
SELECT to_date(20140501130926, 'yyyymmdd h24:mi:ss') FROM dual
 
ORA-01821: date format not recognized

SQL> --add missing h.
SQL> SELECT to_date(20140501130926, 'yyyymmdd hh24:mi:ss') FROM dual;
 
TO_DATE(20140501130926,'YYYYMM
------------------------------
20140501 13:09:26
 
SQL> 

Re: convert char to date [message #616939 is a reply to message #616924] Mon, 23 June 2014 06:35 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Today
a) I learnt something new
b) found out that I need new glasses
Re: convert char to date [message #616944 is a reply to message #616939] Mon, 23 June 2014 08:10 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It would have helped if the OP had supplied the error message in first place, I didn't spot the actual mistake until I ran it myself, on seeing the real error I studied the format string a lot closer.
Previous Topic: assertions
Next Topic: Exchange partition from table at remote database
Goto Forum:
  


Current Time: Fri Apr 26 00:06:28 CDT 2024