I'm trying to convert data in format 2016/06/26 into 26/06/2016 00:00:00
I was trying few option all the time getting error "Invalid months name",
Any idea/advice?
Thanks
select to_date('2016/05/07 00:00:00','mm/dd/yyyy HH24:MI:SS') from dual
In order to convert a string to a date you need to convert it first to a date. Your problems is that you are trying to format a string not a date. So for you specific case it would be:
--convert it first to a date
select to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS')
from dual
--then convert it to a string in the format you want:
select to_char( to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS'),
'mm/dd/yyyy HH24:MI:SS' )
from dual
--since you want it as a date:
--then convert it to a string in the format you want:
select to_date( to_char( to_date('2016/05/07 00:00:00',
'yyyy/mm/dd HH24:MI:SS'),
'mm/dd/yyyy HH24:MI:SS' )
'mm/dd/yyyy HH24:MI:SS' )
from dual
If you want just to convert your string into a date no matter the format, just use the first select I showed. Thanks to #Boneist in comments for pointing it out.
Related
i have these two queries.
to_char(to_date('1970-01-01 **1:00:00**', 'yyyy-mm-dd hh24:MI:SS') + NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC;
SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual
query 2 returns a string that contains 1:00:00 and i would like to use that query inside query 1 to replace the highlighted 1:00:00.
something like this
to_char(to_date('1970-01-01 (SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual)’, 'yyyy-mm-dd hh24:MI:SS') + NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC
Oracle use || to connect string
to_date('1970-01-01'||(SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual), 'yyyy-mm-dd hh24:MI:SS')
try this.
You can simply use the || operator without query as follows:
to_char(to_date('1970-01-01 ' || TO_CHAR(SYSTIMESTAMP, 'tzr') , 'yyyy-mm-dd hh24:MI:SS')
+ NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC;
I have a column LOGIN_DATETIME from LOGIN table
The column is varchar2 has values in two format
20-11-2018 01:00:00
20-07-2018 14:00
I need to derive a varchar value from this column with the following format
dd-mmm-yyyy hh:mi am/pm
Result expected
20-Nov-2018 01:00 AM
20-Jul-2018 02:00 PM
I tried like
Format 1:
SELECT TO_CHAR(TO_DATE(LOGIN_DATETIME, 'DD-mm-yyyy hh:mi:ss'), 'dd-mmm-yyyy hh:mi AM')
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
Format 2:
SELECT TO_CHAR(TO_DATE(LOGIN_DATETIME, 'DD-mm-yyyy hh:mi'), 'dd-mmm-yyyy hh:mi AM')
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
It's not working.
You could use this only one query:
SELECT
CASE
WHEN LENGTH(login_datetime) = 19 THEN TO_CHAR(TO_DATE(login_datetime, 'DD-MM-YYYY HH24:MI:SS'), 'DD-Mon-YYYY HH:MI AM')
WHEN LENGTH(login_datetime) = 16 THEN TO_CHAR(TO_DATE(login_datetime, 'DD-MM-YYYY HH24:MI'), 'DD-Mon-YYYY HH:MI AM')
END AS login_datetime
FROM login
ORDER BY 1;
Tested ok in rextester
Use case and select format based on field length
SELECT
CASE WHEN LENGTH(LOGIN_DATETIME) = 16 THEN TO_CHAR(TO_DATE(LOGIN_DATETIME, 'dd-mm-yyyy hh24:mi'), 'dd-mm-yyyy HH:mi AM')
ELSE TO_CHAR(TO_DATE(LOGIN_DATETIME, 'dd-mm-yyyy hh:mi:ss'), 'dd-mm-yyyy HH:mi AM')
END
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
That was easy one I dunno the syntax of SQL it was mon not mmm in date format
SELECT
TO_char(to_timestamp (LOGIN_DATETIME, 'DD-MM-RRRR HH24:MI:SS.FF'), 'DD-Mon-YYYY HH12:mi AM')
from LOGIN
WHERE LOGIN_DATETIME is NOT null;
OR
SELECT
TO_char(to_timestamp (LOGIN_DATETIME, 'DD-MM-YYYY HH24:MI:SS.FF'), 'DD-Mon-YYYY HH12:mi AM')
from LOGIN
WHERE LOGIN_DATETIME is NOT null;
The both formats work in one query itself.
I have a date column.
select RETAIL_ACQUISITION_DTTM from XXXXX.TABLE_2348
13/07/2018
I want to select the value as such it returns as below,
2018-07-13T00:00:00
so it has to display the date as YYYY-MM-DD with T and then HH24:MI:SS
You need to convert your string (why is it stored as a string?) to a date using a format model that matches the actual string value:
to_date(RETAIL_ACQUISITION_DTTM, 'DD/MM/YYYY')
Having that format model wrong is why you get the 0013 year in your result.
Then you can convert that date back to a string with to_char(), and you can embed the fixed T as a character literal with double quotes, using a format model like 'YYYY-MM-DD"T"HH24:MI:SS':
with TABLE_2348 (RETAIL_ACQUISITION_DTTM) as (
select '13/07/2018' from dual
)
select to_char(to_date(RETAIL_ACQUISITION_DTTM, 'DD/MM/YYYY'),
'YYYY-MM-DD"T"HH24:MI:SS') as RETAIL_ACQUISITION_DTTM
from XXXXX.TABLE_2348;
RETAIL_ACQUISITION_
-------------------
2018-07-13T00:00:00
You could also just ignore that it is a date and use string manipulation:
with TABLE_2348 (RETAIL_ACQUISITION_DTTM) as (
select '13/07/2018' from dual
)
select substr(RETAIL_ACQUISITION_DTTM, 7, 4)
|| '-' || substr(RETAIL_ACQUISITION_DTTM, 4, 2)
|| '-' ||substr(RETAIL_ACQUISITION_DTTM, 1, 2)
|| 'T00:00:00' as RETAIL_ACQUISITION_DTTM
from XXXXX.TABLE_2348;
RETAIL_ACQUISITION_
-------------------
2018-07-13T00:00:00
If the column is actually a date rather than a string then you are doing unnecessary conversions, including implicit ones which rely on your NLS settings, and you are losing the original time from the value if it was not midnight anyway:
alter session set nls_date_format = 'DD-MON-RR';
with TABLE_2348 (RETAIL_ACQUISITION_DTTM) as (
select to_date('2018-07-13 12:34:56', 'YYYY-MM-DD HH24:MI:SS') from dual
)
select to_char(to_date(RETAIL_ACQUISITION_DTTM, 'YYYY-MM-DD-HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS') as RETAIL_ACQUISITION_DTTM from XXXXX.TABLE_2348;
RETAIL_ACQUISITION_
-------------------
0013-07-20 18:00:00
That is really doing:
to_char(
to_date(
to_char(
RETAIL_ACQUISITION_DTTM,
'DD-MON-RR'), ---- from your session NLS_DATE_FORMAT setting
'YYYY-MM-DD-HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS')
If you skip to extra steps you can just format the date directly:
with TABLE_2348 (RETAIL_ACQUISITION_DTTM) as (
select to_date('2018-07-13 12:34:56', 'YYYY-MM-DD HH24:MI:SS') from dual
)
select to_char(RETAIL_ACQUISITION_DTTM,
'YYYY-MM-DD"T"HH24:MI:SS') as RETAIL_ACQUISITION_DTTM
from XXXXX.TABLE_2348;
RETAIL_ACQUISITION_
-------------------
2018-07-13T12:34:56
which also doesn't rely on your NLS settings, so won't break in interesting ways if it's run in another session with different settings.
replace sysdate with your column name if its date type.
Using Single to_char:
select to_char(sysdate,'--YYYY-MM-DD"T"hh24:mi:ss') from dual;
Using two to_char and concatenation for simplification.
select to_char(sysdate,'--'||'YYYY-MM-DD')||'T'||to_char(sysdate,'hh24:mi:ss') from dual; -- 13/07/2018
select '--'||to_char(sysdate,'YYYY-MM-DD')||'T'||to_char(sysdate,'hh24:mi:ss') from dual; -- 13/07/2018
I've a Oracle query like this:
SELECT * FROM
WHERE
CODE='AMB01'
AND MyHour BETWEEN
TO_DATE('11/01/2018 16:00:00', 'DD/MM/YYYY hh24:mi:ss') AND
TO_DATE('11/01/2018 16:30:00', 'DD/MM/YYYY hh24:mi:ss')
It return an error :
Month not valid
Myhour is a String field.
What can be the error ?
Thanks
Edited:
You need to convert MyHour, not between part.
SELECT * FROM
WHERE
CODE='AMB01'
AND TO_DATE(MyHour, 'DD/MM/YYYY hh24:mi:ss') BETWEEN
'11-01-2018 16:00:00' AND
'11-01-2018 16:30:00'
if MyHour type is different , Set column type as Date
UPDATE_DATETIME DATE
SELECT * FROM YourTable
WHERE MyHour BETWEEN
TO_DATE('11/01/2018 16:00:00', 'DD/MM/YYYY hh24:mi:ss') AND
TO_DATE('11/01/2018 16:30:00', 'DD/MM/YYYY hh24:mi:ss')
Need to convert date format from (source oracle) yyyy/mm/dd to (target - SQL Server) mm/dd/yyyy.
Current Query:
SELECT* FROM WVT.WVCAS
WHERE to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss', 'mm/dd/yyyy hh24:mi:ss')
Getting error:
ORA-00920: invalid relational operator
Please help.
"Invalid relational operator" usually means that you have a where clause without a comparison. In your case, the where clause has the conversion to_date(), but no comparison. Perhaps you mean something like:
SELECT *
FROM WVT.WVCAS
WHERE dttmcutpull > sysdate - 1;
In other words, merely converting the data is not sufficient, you have to compare it to something.
If you just want to do the conversion, then put that in the select:
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
EDIT:
You have to convert each column independently, not all at once. to_char() takes two arguments, a date and a format:
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as date1,
to_char(DTTMPULLll, 'mm/dd/yyyy hh24:mi:ss') as date2,
to_char(DTTMRUNll, 'mm/dd/yyyy hh24:mi:ss') as date3,
to_char(SYSLOCKDATEll, 'mm/dd/yyyy hh24:mi:ss') as date4,
to_char(SYSMODATEll, 'mm/dd/yyyy hh24:mi:ss') as date5,
to_char(SYSCREATEDATE 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
SELECT w.*,
TO_CHAR(to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
What I am doing is,
dttmcutpull is a STRING (VARCHAR) with format yyyy/mm/dd hh24:mi:ss
Initially converting the string to a DATE using TO_DATE
Re-converting the DATE to a string of mm/dd/yyyy hh24:mi:ss using TO_CHAR function
Used Syntax is similar to
SELECT
TO_CHAR
(
TO_DATE (STRING_FIELD_VALUE, EXISTING_FORMAT_OF_STRING_FIELD_VALUE),
EXPECTED_FORMAT_OF_NEW_VALUE
) AS NEW_STRING_FIELD_VALUE;
EDIT: Your Query should be:
If dttmcutpull, DTTMPULLll,DTTMRUNll etc are DATE datatypes then
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE1,
to_char(DTTMPULLll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE2,
to_char(DTTMRUNll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE3,
to_char(SYSLOCKDATEll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE4,
to_char(SYSMODATEll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE5,
to_char(SYSCREATEDATE 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE6
FROM WVT.WVCAS w;
If dttmcutpull, DTTMPULLll,DTTMRUNll etc are VARCHAR(STRING/CHARACTER) datatypes then
SELECT w.*,
TO_CHAR(to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE1,
TO_CHAR(to_date(DTTMPULLll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE2,
TO_CHAR(to_date(DTTMRUNll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE3,
TO_CHAR(to_date(SYSLOCKDATEll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE4,
TO_CHAR(to_date(SYSMODATEll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE5,
TO_CHAR(to_date(SYSCREATEDATE , 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE6
FROM WVT.WVCAS w;