SQL: Convert varchar to date - sql

I have the following varchar type variable that I need to convert to date:
2019-07-24T13:52:04
When I do the following select:
SELECT TO_CHAR(TO_DATE(createddt,'YYYY-MM-DD T HH:MM:SS'), 'YYYY-MM-DD T HH:MM:SS')
FROM tbl$orders;
it gives me an error message:
date format not recognized.
How to convert this varchar to date?

I am going to speculate that you are using Oracle because it has to_date() and in Oracle, the date data type has a time component.
This then works:
select to_date('2019-07-24T13:52:04', 'YYYY-MM-DD"T"HH24:MI:SS')
from dual;

Is that what are you looking for
SELECT TO_DATE(REPLACE('2019-07-24T13:52:04', 'T', ' '), 'YYYY-MM-DD HH24:MI:SS')
FROM Dual;

Actually, Date format in Oracle is defined by NLS_DATE_FORMAT and you need to set it according to your requirement.
You are not able to see the letter T as your NLS_DATE_FORMAT is not set like that way.
You can try the following example for your problem:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS'; -- setting the date format to be displayed at the session level
SELECT
TO_DATE('2019-07-24T13:52:04', 'YYYY-MM-DD"T"HH24:MI:SS')
FROM
DUAL; -- Your query
Also, Please read about NLS_DATE_FORMAT from oracle documentation. It is really needed to understand how dates are stored and how it is displayed in oracle.
Cheers!!

Related

'ORA-01843: not a valid month' but its in the right format?

this is for ORACLE SQL DEVELOPER
I have a column called 'date' and its in the format Timestamp(0). I do the following beforehand.
alter session set nls_date_format = 'MM/DD/YYYY HH:MI' an example data set of my date is the following
'01/14/2020 08:00'
but when I insert into the table like below
INSERT INTO MY_TABLE
VALUES('01/14/2020 08:00',1,1,1,1,1,1,1,1,1,1,1)
I get the above error "ORA-01843: not a valid month" any ideas what the issue might be? it's in the right format, and being inserted via python cx_Oracle, feel like im missing something trivial?
You can use to_timestamp() function :
to_timestamp('01/14/2020 08:00','MM/DD/YYYY HH24:MI')
Demo
You're trying to insert a string into a timestamp column in the case of the question.
Update : If proper formatting applied through use of nls_timestamp_format such as
alter session set nls_timestamp_format = 'MM/DD/YYYY HH24:MI';
then no problem would raise by using the string without to_timestamp() function.
Demo2
Use the timestamp keyword to add in a timestamp constant:
timestamp '2020-01-14 08:00:00'
You can use date to provide a date constant and timestamp for a timestamp constant.

Converting timestamp to date in Oracle 12c

I want to convert a given timestamp in such format: 2019-04-08 00:00:00.0 to a date in this format: 2019-04-08.
I have already tried using:
SELECT TO_DATE('2019-04-20 00:00:00.0','YYYY-MM-DD') from dual;
But I got prompted with:
ORA-01830: date format picture ends before converting entire input
string
I think you may have some conceptual misunderstanding about how the TO_DATE function works, and also about how dates are processed by the DBMS.
YYY-MM-DD does not match the format of the actual string you're importing (2019-04-20 00:00:00.0) That's what the error is telling you. You must tell the TO_DATE function what to expect in the date string you input into it. You do that by means of the format string. if you don't specify a format string which matches the format you're actually going to supply, then the function will fail to process the string.
Next, you say you want to convert it "to a date in this format"...but this does not entirely make sense. TO_DATE converts a string into a variable of type DATETIME - i.e. a date object. A date object does not not exist in any particular format, it exists as an object. Internally it will store the date information in a way which is independent of any human-readable date format. The format relates entirely to the presentation of the date when seen as a string. Once you have a date object, you can then output the date in a particular format if you want to a human to be able to read it in the style that their culture is familiar with.
So, firstly to import your date string correctly as a date object, you can use an accurate format string, an also use TO_TIMESTAMP instead of TO_DATE so that it captures the sub-seconds value:
SELECT TO_TIMESTAMP('2019-04-20 00:00:00.0','YYYY-MM-DD HH24:MI:SS.FF5') from dual;
If you run this in a console the SELECT will then automatically re-format that date object (the result of the TO_DATE function) into the default date format configured in your server / session.
However if you actually want to see it on screen in a particular format, you can explicitly say so - a sensible way is using the TO_CHAR function:
SELECT TO_CHAR(TO_TIMESTAMPT('2019-04-20 00:00:00.0','YYYY-MM-DD HH24:MI:SS.FF5'), 'YYYY-MM-DD') from dual;
The full list of format specifiers can be found here (and in other places online as well).
Live demo of the above here: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=619d918ea73953e11b3150c6b560112c
Assuming the input is actual text, and not a real timestamp, you could try just truncating the text before you call TO_DATE:
WITH cte AS (
SELECT '2019-04-20 00:00:00.0' AS ts FROM dual
)
SELECT TO_DATE(SUBSTR(ts, 1, 10), 'YYYY-MM-DD')
FROM cte;
If your input is an actual Oracle timestamp, and you want to convert it to a date, then you may use CAST:
SELECT CAST(ts AS DATE) dt
FROM cte;
Would CAST do any good?
I'm setting date format so that it displays time component, although it is 00:00:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select cast(timestamp '2019-04-20 00:00:00.0' as date) result from dual;
RESULT
-------------------
20.04.2019 00:00:00
Another format (without time component):
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> select cast(timestamp '2019-04-20 00:00:00.0' as date) result from dual;
RESULT
----------
20.04.2019
SQL>
Or, using TO_CHAR function (so that session's date format doesn't matter):
SQL> select to_char(timestamp '2019-04-20 00:00:00.0', 'dd.mm.yyyy') result from dual;
RESULT
----------
20.04.2019
SQL>

Convert string to date in Oracle SQL

I'm trying to convert string column to date in Oracle SQL.
Here is my an example of value from the data:
'03/12/14 11:00:00'
Here is my query:
select to_date(A,'DD/MM/YY HH24:MI:SS')
from MyTable
Here is an example of the output:
03-DEC-14
Why my query return only the date but not the hour?
Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
To adjust this, run:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
Then you should see the time as well.
You are trying to DISPLAY the datetime value. Use TO_CHAR along with proper FORMAT MODEL.
For example,
select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
A DATE consists of both date and time portions. You just need to make sure you use appropriate format model to display it. Else, you end up with a format which depends on your locale-specific NLS settings.
You can use timestamp
select to_timestamp(A, 'DD/MM/YY HH24:MI:SS') from MyTable
If you want a query that returns the time portion of the date without having to alter the nls_date_format you can convert the date you got from the to_date function to a text representation (the default text representation is dd-Mon-yy as you found out) like this:
select to_char(
to_date(A,'DD/MM/YY HH24:MI:SS'),
''DD/MM/YY HH24:MI:SS')
from MyTable;
Ideally, column A of MyTable should have been defined as a date column instead of a varchar2 column. There are several reasons to do that; data integrity, and better SQL optimization.

Date conversion in Oracle not working as expected

ORACLE 10 g db: convert date format from
02-JUL-14
to
02/JUL/14
I tried using the the select query below to get "02/JUL/14" instead it shows "02-JUL-14":
SELECT ROUTINGNUM , to_date (EFFDATE,'DD/MM/YYYY') FROM hat;
Can anybody please help reading this.
if EFFDATE is a date column,
SELECT ROUTINGNUM , to_char( EFFDATE,'DD/MON/YYYY') FROM hat;
if it is a String in the format DD-MON-YY
SELECT ROUTINGNUM , to_char( to_date( EFFDATE,'DD-MON-YY') ,'DD/MON/YYYY')
FROM hat;
TO_DATE function is used for converting from String to Date type. Use TO_CHAR function instead.
If your EFFDATE field is already a date type, then you just need to change NLS_DATE_FORMAT.
alter session set NLS_DATE_FORMAT = 'DD/MM/YYYY';
select eff_date from hat;
Or use TO_CHAR on EFFDATE
select to_char(eff_date, 'DD/MM/YYYY') from hat;
If you typically deal with a certain format of date, then you can change the default for your instance by setting one or more of the NLS init parameters.
sqlplus / as sysdba
show parameter nls

How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL?

I have a date column in a table stored as MM/DD/YYYY format. I have to select and store the same date in another table in YYYY-MM-DD format i.e. XSD Date Format. But I am not able to do it. I am using this query:
select to_date(date_column,'YYYY-MM-DD') from table;
But still I am not able to do it. Giving me error
ORA-01843 : not a valid month
use
select to_char(date_column,'YYYY-MM-DD') from table;
It sounds like you've got it the wrong way round. If your existing data is in MM/DD/YYYY format, then you want:
select to_date(date_column,'MM/DD/YYYY') from table;
to convert the existing data to DATE values. (I do wonder why they're not stored as dates, to be honest...)
If you want to perform the conversion in one step, you might want:
select to_char(to_date(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD') from table;
In other words, for each row, parse it in MM/DD/YYYY format, then reformat it to YYYY-MM-DD format.
(I'd still suggest trying to keep data in its "natural" type though, rather than storing it as text in the first place.)
I assume that you can use the Oracle SQL Developer, which you can download from here.
You can define the date format which you want to work with:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
With this, now you can perform a query like this:
SELECT * FROM emp_company WHERE JDate = '2014-02-25'
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
To convert a DATE column to another format, just use TO_CHAR() with the desired format, then convert it back to a DATE type:
SELECT TO_DATE(TO_CHAR(date_column, 'DD-MM-YYYY'), 'DD-MM-YYYY') from my_table
select to_date(to_char(ORDER_DATE,'YYYY/MM/DD'))
from ORDERS;
This might help but, at the end you will get a string not the date. Apparently,
your format problem will get solved for sure .
For military time formatting,
select TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mm:ss') from DUAL
--2018-07-10 15:07:15
If you want your date to round DOWN to Month, Day, Hour, Minute, you can try
SELECT TO_CHAR( SYSDATE, 'yyyy-mm-dd hh24:mi:ss') "full-date" --2018-07-11 10:40:26
, TO_CHAR( TRUNC(SYSDATE, 'year'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-year"-- 2018-01-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'month'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-month" -- 2018-07-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'day'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-Sunday" -- 2018-07-08 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'dd'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-day" -- 2018-07-11 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'hh'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-hour" -- 2018-07-11 10:00:00
, TO_CHAR( TRUNC(SYSDATE, 'mi'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-minute" -- 2018-07-11 10:40:00
from DUAL
For formats literals, you can find help in
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions242.htm#SQLRF52037
You can do this simply by :
select to_char(to_date(date_column, 'MM/DD/YYYY'), 'YYYY-MM-DD') from table
According to the comments, the data-type in the datatable is DATE.
So you should simply use:
"select date_column from table;"
Now if you execute the select you will get back a date data-type, which should be what you need for the .xsd.
Culture-dependent formating of the date should be done in the GUI (most languages have convenient ways to do so), not in the select-statement.
Basically , Data in a Date column in Oracle can be stored in any user defined format or kept as default.
It all depends on NLS parameter.
Current format can be seen by : SELECT SYSDATE FROM DUAL;
If you try to insert a record and insert statement is NOT in THIS format then it will give :
ORA-01843 : not a valid month error.
So first change the database date format before insert statements ( I am assuming you have bulk load of insert statements) and then execute insert script.
Format can be changed by :
ALTER SESSION SET nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
Also You can Change NLS settings from SQL Developer GUI , (Tools > preference> database > NLS)
Ref: http://oracle.ittoolbox.com/groups/technical-functional/oracle-sql-l/how-to-view-current-date-format-1992815
This worked for me! You can convert to datatype you want be it a date or string
to_char(TO_DATE(TO_CHAR(end_date),'MM-DD-YYYY'),'YYYY-MM-DD') AS end_date
Late reply but for.databse-date-type the following line works.
SELECT to_date(t.given_date,'DD/MM/RRRR') response_date FROM Table T
given_date's column type is Date
Just to piggy back off of Yahia, if you have a timestamp you can use this function to cast exclusively as date, removing the timestamps.
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYY-MM-DD') AS TrackerKey__C
Or in my case I need the below format
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYYMMDD') AS TrackerKey__C
SELECT TO_DATE(TO_CHAR(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD')
FROM table;
if you need to change your column output date format just use to_char this well get you a string, not a date.
use
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
also gothrough
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html