convert string literal to a date - sql

I have a varchar2 field in my db with the format of for example -
2015-08-19 00:00:01.0
2014-01-11 00:00:01.0
etc.
I am trying to convert this to a date of format DD-MON-YYYY. For instance, 2015-08-19 00:00:01.0 should become 19-AUG-2015. I've tried
select to_date(upgrade_date, 'YYYY-MM-DD HH24:MI:SS') from connection_report_update
but even at this point I'am getting ORA-01830 date format ends before converting the entire input string. Any ideas?

You have details upto milli seconds, for which, you have to use TO_TIMESTAMP() with format model 'FF'
select to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF') as result from dual;
RESULT
---------------------------------------------------------------------------
19-AUG-15 12.00.01.000000000 AM
And Date doesn't have a format itself, only the date output can be in a format. So, when you want it to be printed in a different format, you would need to again use a TO_CHAR() of the converted timestamp;
select to_char(to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY') as result from dual;
RESULT
-----------
19-AUG-2015

Why do you store datetimes in a string???
Anyhow. To get from '2015-08-19 00:00:01.0' to a datetime with milliseconds (which is a TIMESTAMP in Oracle) use to_timestamp:
to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff')
Then to get the desired output format, use to_char:
to_char(thedate, 'DD-MON-YYYY')
Together:
to_char(to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff'), 'DD-MON-YYYY')

You should be specifying the format that you want in the call of to_date not the current format:
select to_date(upgrade_date, 'DD-MM-YYYY') from connection_report_update

Related

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

PL SQL - Convert timestamp to datetime/date

select
to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as SCHEDULED_TIME,
TRUNC(to_date(to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF'),'YYYY-MM-DD HH24:MI:SS'))
from S_TIDAL_STATUS
The error was:
ORA-01830: date format picture ends before converting entire input string
01830. 00000 - "date format picture ends before converting entire input string"
The goal is to return something like
2017-07-91 23:14:00
(without the content after the dot).
Here's what the SCHEDULED_TIME (timestamp) looked like:
The problem in your attempt is the function TO_DATE() applied to a timestamp. TO_DATE() takes a VARCHAR2 (string) input, not a timestamp. So Oracle converts the timestamp to a string first, implicitly, using your NLS_TIMESTAMP_FORMAT parameter, and then attempts to convert this string to a date. Depending on your NLS_TIMESTAMP_FORMAT, you may get different errors.
The way to convert a timestamp to a date (datetime) - while truncating off the fractions of a second - is with the CAST function. Example:
select systimestamp,
cast (systimestamp as date) as ts_cast_to_date
from dual
;
Alternatively, if all your strings are EXACTLY in that format, you can truncate the strings first and apply TO_DATE directly:
to_date(substr(scheduled_time, 1, 19), 'yyyy-mm-dd hh24:mi:ss')
This should do the trick:
select
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as time_to_csecs,
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS') as time_to_secs,
TRUNC(to_date(to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')) as time_to_day
from S_TIDAL_STATUS
Please review the docs to see the difference between to_timestamp and to_char.

How to convert date to datetime in Oracle?

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI how can i proceed?
I've tried this but nothing is working :
to_date(the_date,'DD-MM-YYY HH24:MI')
and also this:
to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')
i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI
No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 bytes with each byte storing different components of the datetime value.
DATE data type always has both date and time elements up to a precision of seconds.
If you want to display, use TO_CHAR with proper FORMAT MODEL.
For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42
Oracle DATE datatype ALWAYS contains (stores) time.
If you want to see it, you can use function TO_CHAR.
If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.
If you want to covert to timestamp, you can do the following:
Select to_timestamp(date_column, 'DD-MM-YYY') from table;
However, if you want in the required format, you can do the following:
Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI')
from table;
Hope it helps..

to_date unable to print timestamp

I want to print the timestamp from the below sql
select to_date('01/01/2011 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM') from dual;
current output --> 1/1/2011 (not printing the timestamp only for 12 am. if the min is 12:01 then it is printing.
but I need the output as 1/1/2011 12:00:00 AM
TO_DATE converts a string to a DATE. A DATE is stored in a packed binary format that is not human readable. An Oracle DATE does not have a format. So when you ask a program to display a date, it has to then convert the DATE to a string. If you don't explicitly specify the format by doing an explicit TO_CHAR, a tool like SQL*Plus will convert the date to a string using the session's NLS_DATE_FORMAT. Other applications may choose different ways to convert a date to a string-- using the client's regional settings, for example, or by allowing the user to configure the format.
If you want to return a string in a particular format that represents a DATE, you'd need to use an explicit TO_CHAR. Something like
SELECT to_char( some_date_column, 'dd/mm/yyyy hh:mi:ss AM' )
FROM some_table
In the specific case you posted, since you have the string in your hand as a string, you'd simply want to select it from dual rather than doing a TO_DATE to convert it to a date and then a TO_CHAR to convert it back to a string. I'm assuming, though, that you have an actual DATE in the actual table that you are trying to select from.
The best way to control the formatting is to use to_char and explicitly specify the date format you want.
select to_char(to_date('01/01/2011 12:00:00 AM','dd/mm/yyyy hh:mi:ss AM'),'DD/MM/yyyy hh:mi:ss AM')
from dual;
You can Use
select Convert(varchar,'01/01/2011 12:00:00 AM',113)

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